문제

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)

도움이 되었습니까?

해결책 3

Use cut:

cut -d, -f2- file

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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top