Simon
Simon

Reputation: 1416

Removing line numbers (not entire line) from a file in unix

How can I remove line numbers from a file if the line numbers have been added by 'nl'?

example:

file1:

   word1 word2
   word3
   word4

After command: nl file1 > file2 This is the exact command used.

file2:

1 word1 word2
2 word3
3 word4

Here comes the part where it revolves around. Removing the line numbers from file2 and storing the lines in file3 (Or if possible, removing the numbers in file 2 whilst keeping the lines in file 2).

file3:

   word1 word2
   word3
   word4

Upvotes: 2

Views: 5878

Answers (6)

Victor Burenkov
Victor Burenkov

Reputation: 187

This will remove only the first word/number from each string in file2 and put the rest in file3:

awk '{$1 = ""; print $0;}' file2 > file3

file3:

 word1 word2
 word3
 word4

Upvotes: 1

David Arnold
David Arnold

Reputation: 455

The cut utility will work. In this case, you have only one word in the line, so you can use just cut -f2, but if you had more columns, cut -f2- will preserve all except the first.

Upvotes: 2

Damell
Damell

Reputation: 59

Something like this should solve it:

cut -d\  -f2- < file1

Upvotes: 1

Beta
Beta

Reputation: 99094

sed 's/ *[0-9]*.//' file2 > file3

Upvotes: 5

Oleg S Kleshchook
Oleg S Kleshchook

Reputation: 464

Yep. As it was answered here: you can use awk:

cat file | awk '{print $2}' > newfile

you can use cut:

cat file | cut -f2 > newfile

Upvotes: 3

suspectus
suspectus

Reputation: 17268

Assuming you can't just cp file1 file3....

ni file1 > file2
sed 's/[0-9]*[ ]*\(.*\)$/\1/' file1 > file3

Upvotes: 0

Related Questions