sed : Second field not getting displayed in the output

Hi ,

I have a file (input.txt) with the below contents :

20150223T12:00:25 hostnamex
20150716T10:40:54 hostnamey
20150202T20:08:03 hostnamez

I want the output to be like below i.e excluding the timestamp ( THH:MM:SS) in the above file as follows :

20150223 hostnamex
20150716 hostnamey
20150202 hostnamez

I have tried the following sed command but it is giving only date and the second field is not getting displayed :

sed -n 's/T.*\([^[:blank:]]*\).*\([^[:blank:]]*\).*/\1 \2/p' input.txt

Output :

20150223
20150716
20150202

Could someone please help me and explain it. Thanks a lot :slight_smile: .

Rahul

Why so complicated? Try

sed -n 's/T[^[:blank:]]*//p' file
20150223 hostnamex
20150716 hostnamey
20150202 hostnamez

Your snippet replaces anything after T with a space ( .* matches everything until EOL; \1 is null or more occurrences of a non-blank char, matching null; same does \2 )

1 Like

Hello rahul2662,

If you are interested to do it with awk . Then following may help you in same.

 awk '{sub(/T.*[[:space:]]/," ",$0);print}'  Input_file
 

Thanks,
R. Singh

1 Like