Question

The content of my file.txt is something like this:

fghfg
sdfsd
thrt
wefs
dfd
htyj
dfd
gsedf
     1     sdfsdf
     3     sdfghj
     f     fgdfhtyu
dfdsfsd
sdgdfg
sdfzcxzc
fgjty

I want to change this part:

1     sdfsdf
3     sdfghj
f     fgdfhtyu

to this one: (by removing first column and spaces)

sdfsdf
sdfghj
fgdfhtyu

Then redirect the whole output of file.txt to file2.txt.

How is it possible...?

Thanks

Was it helpful?

Solution 3

This should do:

awk '{print $NF}' file.txt > file2.txt

or shortest solution of the day:

awk '{$0=$NF}1' file.txt > file2.txt
cat file2.txt
fghfg
sdfsd
thrt
wefs
dfd
htyj
dfd
gsedf
sdfsdf
sdfghj
fgdfhtyu
dfdsfsd
sdgdfg
sdfzcxzc
fgjty

Just print the last column.

OTHER TIPS

Try changing your script to this:

find "$1" -print |
while IFS= read -r line; do
    awk '!a[$0]++{print $NF}' "$line" > /tmp/file.txt
done

Does anything need to change?

Using sed, you can do something like this

sed 's/^ \+[^ ] \+//' file1.txt > file2.txt

It will remove all sequences at the begining of the line that contain a sequence of whitespaces, followed by a single non whitespace character than a sequence of whitespaces. Using the file in your question, the results is:

fghfg
sdfsd
thrt
wefs
dfd
htyj
dfd
gsedf
sdfsdf
sdfghj
fgdfhtyu
dfdsfsd
sdgdfg
sdfzcxzc
fgjty

Another simple sed command,

sed 's/.* \(.*\)/\1/g' file1.txt > file2.txt
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top