0

I have two columns like below

0:0:36 hostname-x.y.com:1212/dbname
server.y.com:1012/bname
0:0:9 commom.y.com:9312/xname
windows.y.com:1052/wname

Desire output should be as follows

hostname-x.y.com:1212/dbname
server.y.com:1012/bname
commom.y.com:9312/xname
windows.y.com:1052/wname

Basically, when it finds two columns, it should skip the first column. I have nearly 10k lines. Any suggestions would be a great help.

0

4 Answers 4

2

That can trivially be done with awk simply with:

awk '{print $NF}' filename

Which will simply print the last field on each line.

Example Use/Output

With your input file in file:

$ awk '{print $NF}' file
hostname-x.y.com:1212/dbname
server.y.com:1012/bname
commom.y.com:9312/xname
windows.y.com:1052/wname
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @RavinderSingh! (funny how I grab ` instead of ' on occasion -- must be a SO thing... :)
1

By seeing your sample of expected output(adding more generic solution) if I get it correctly(you want to print data after digits), could you please try following.

awk 'match($0,/[0-9]+:[0-9]+:[0-9]+ +/){print substr($0,RSTART+RLENGTH);next} 1' Input_file

Explanation: Adding detailed explanation for above code.

awk '                                    ##Starting awk program from here.
match($0,/[0-9]+:[0-9]+:[0-9]+ +/){      ##Using match function which matches regex of digits colon digits colon digits then space in line.
  print substr($0,RSTART+RLENGTH)        ##If a match is found then it should print sub-string of line starting from RSTART till RLENGTH.
  next                                   ##next will skip all further statements from here.
}
1                                        ##1 will print edited/non-edited lines here.
'  Input_file                            ##Mentioning Input_file name here.

Comments

0
sed 's/.* //g' < input_file

Demo


:>cat test.txt
0:0:36 hostname-x.y.com:1212/dbname
server.y.com:1012/bname
0:0:9 commom.y.com:9312/xname
windows.y.com:1052/wname
:>sed 's/.* //g' test.txt
hostname-x.y.com:1212/dbname
server.y.com:1012/bname
commom.y.com:9312/xname
windows.y.com:1052/wname
:>



Comments

0

Use cut to take the second filed, with " " as delimiter. If the delimiter is not found, the entire line is given in output, as specified by the -f option help section (cut --help)

-f, --fields=LIST       select only these fields;  also print any line
                            that contains no delimiter character, unless
                            the -s option is specified

The command should be

cut -f2 -d" " yourfile

Demo:

root@a036fb1c94fa:~# cat yourfile
0:0:36 hostname-x.y.com:1212/dbname
server.y.com:1012/bname
0:0:9 commom.y.com:9312/xname
windows.y.com:1052/wname 

root@a036fb1c94fa:~# cut -f2 -d" " yourfile
hostname-x.y.com:1212/dbname
server.y.com:1012/bname
commom.y.com:9312/xname
windows.y.com:1052/wname

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.