2

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)

2
  • simple awk -F, '{print $2}' file will does the job for you. Commented May 11, 2014 at 13:34
  • I want to print all columns , it just happened here I have only 2 columns. Commented May 11, 2014 at 13:37

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

Comments

3

Another awk

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

Removes the first field, then the separator.

Comments

2

Use cut:

cut -d, -f2- file

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.