sed : Second field not getting displayed in the output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed : Second field not getting displayed in the output
# 1  
Old 07-21-2015
sed : Second field not getting displayed in the output

Hi ,

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

Code:
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 :

Code:
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 :

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

Output :

Code:
20150223
20150716
20150202

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

Rahul
# 2  
Old 07-21-2015
Why so complicated? Try
Code:
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 )
This User Gave Thanks to RudiC For This Post:
# 3  
Old 07-21-2015
Hello rahul2662,

If you are interested to do it with awk. Then following may help you in same.
Code:
 awk '{sub(/T.*[[:space:]]/," ",$0);print}'  Input_file

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Combine Similar Output from the 2nd field w.r.t 1st Field

Hi, For example: I have: HostA,XYZ HostB,XYZ HostC,ABC I would like the output to be: HostA,HostB: XYZ HostC:ABC How can I achieve this? So far what I though of is: (1 Reply)
Discussion started by: alvinoo
1 Replies

2. Shell Programming and Scripting

Perl : blank lines are displayed in the output after deleting few rows from excel

I am working on an assignment to pull all the records from excel sheet programatically and use the data for further calculations. In this process, I first defined 10 records in excel sheet and executed the below code. In the first run it is OK. But after deleting last few rows in excel sheet and... (0 Replies)
Discussion started by: giridhar276
0 Replies

3. Shell Programming and Scripting

Plz Help. Compare 2 files field by field and get the output in another file.

Hi Freinds, I have 2 files . one is source.txt and second one is target.txt. I want to keep source.txt as baseline and compare target.txt. please find the data in 2 files and Expected output. Source.txt 1|HYD|NAG|TRA|34.5|1234 2|CHE|ESW|DES|36.5|134 3|BAN|MEH|TRA|33.5|234... (5 Replies)
Discussion started by: i150371485
5 Replies

4. Shell Programming and Scripting

Compare two files Field by field and output the result in another file

Hi Friends, Need Help. I have file1.txt as File1.txt |123|A|7267|Hyder|Cross|Sell|7801 |995|A|7051|2008|Lunar|New|Year|Promotion|7801 |996|A|7022|Q108|Targ|Prospect|&|SSCC|Savings|Promo|7801 |997|A|7182|Q1|Feb-Apr|08|Credit|ITA|PA|SBA|Campaign|7801 File2.txt... (7 Replies)
Discussion started by: i150371485
7 Replies

5. Shell Programming and Scripting

sed to replace a field from a line with another field

i have something like this, cat filename.txt hui this si s"dfgdfg" omeone ipaddress="10.19.123.104" wel hope this works i want to replace only 10.19.123.104 with different ip say 10.19.123.103 i tried this sed -i "s/'ipaddress'/'ipaddress=10.19.123.103'/g" filename.txt ... (1 Reply)
Discussion started by: vivek d r
1 Replies

6. Shell Programming and Scripting

perl - output not being displayed

Hi All I have the following code sub pall_nvrm { my $cmd = `pall -w "/u/ab/scripts/dev/nvrmerros/nvrmpaller"`; print $cmd; } if i run pall -w "/u/ab/scripts/dev/nvrmerros/nvrmpaller" in a shell i get this sort of out put eagley: boxted: cadle: eabost: hales: (3 Replies)
Discussion started by: ab52
3 Replies

7. Shell Programming and Scripting

ftp:Password field is displayed

I am trying to get a file by doing ftp to the server. The below script works fine but as I type password it is displayed as simple text. HOST='192.108.245.101' echo 'username: ' read USER echo 'password:' read PASSWD ftp -n $HOST <<END_SCRIPT quote USER $USER quote PASS $PASSWD lcd... (1 Reply)
Discussion started by: hiten.r.chauhan
1 Replies

8. Shell Programming and Scripting

Trying to use sed to remove the value of one field from another field

I'm trying to use sed to remove the value of one field from another field. For example: cat inputfile 123|ABC|Generic_Textjoe@yahoo.com|joe@yahoo.com|DEF 456|GHI|Other_recordjohn@msn.com|john@msn.com|JKL 789|MNO|No_Email_On_This_One|smith@gmail.com|PQR I would like to remove the email... (2 Replies)
Discussion started by: bribri87
2 Replies

9. Shell Programming and Scripting

Output file not displayed in the proper format

Hi am using uuencode fro attaching one report which is nothing but sql query output. But when i receive the report through attachement and when it is opened the report is not displayed in proper format. Means if the sql query has 100 rows the mail attachment report displays the report in 2... (2 Replies)
Discussion started by: weknowd
2 Replies

10. Shell Programming and Scripting

adding another field to SED output

Dear experts, I have a file called "check" with contents like below i used the sed command like below to get the value of "success" and "failed" only My question is how can i get the value to include the time "03:15", so that i can get a value such as below : - Appreciate... (4 Replies)
Discussion started by: aismann
4 Replies
Login or Register to Ask a Question