Question

I have a file named test.txt which has the following content
123,2,1
I want to print the contents of the file without "123,"
I used the following
awk -F, '{print substr($0, index($0, $2))} text.txt'

It doesn't work properly tho, it prints 23,2,1
But when I run it for this text '123,5,1' , it works without a problem (it prints 5,1)

Was it helpful?

Solution 3

Use cut:

cut -d, -f2- file

-d, uses , as delimiter, and -f2- prints fields 2 to last.

OTHER TIPS

You can use sub() function and remove the first field regardless of what it is.

$ echo '123,2,1' | awk '{sub(/[^,]+,/,"")}1' 
2,1

$ echo '23,2,1' | awk '{sub(/[^,]+,/,"")}1'
2,1

Another awk

echo '123,2,1' | awk -F, '{$1="";sub(/,/,"")}1' OFS=,
2,1

Removes the first field, then the separator.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top