Trim using sed/awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trim using sed/awk
# 1  
Old 12-21-2018
Trim using sed/awk

How to remove using the sed/awk if the rows are starting with any numbers till "-" character. from the below sample output and expected results.

Sample looks like below :

Code:
704 - sample - test
5500 - line2
449 - line3
44 - line4
Line5

Expected -

Code:
sample - test
line2
line3
line4
Line5


Last edited by vgersh99; 12-21-2018 at 11:40 AM.. Reason: Code tags, please!
# 2  
Old 12-21-2018
Code:
sed 's/^[0-9]\+\s*-\s*//'

# 3  
Old 12-21-2018
Code:
awk '{ sub(/^[0-9]* - /,"") } 1' inputfile > outputfile

These 2 Users Gave Thanks to Corona688 For This Post:
# 4  
Old 12-21-2018
Thanks, If the file is having three columns with delimiter "|" and i need to the trim only on column 3. Can you please help on this?

Code:
column1|column2|column3
1|aa1|704 - sample - test
2|aa2|5500 - line2
3|aa3|449 - line3
4|aa4|44 - line4
 5|aa5|Line5




Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 12-21-2018 at 11:58 AM.. Reason: Added CODE tags.
# 5  
Old 12-21-2018
1) Describe your ultimate problem right in the first post so not to waste (your and others') time with inapt solutions!
2) Seriously: Use CODE tags!



Rg. your new problem:

Adapt Corona668's proposal to work on the third field, and supply the field separators:


Code:
awk -F\| '{ sub(/^[0-9]* - /,"", $3) } 1' OFS=\| file3
column1|column2|column3
1|aa1|sample - test
2|aa2|line2
3|aa3|line3
4|aa4|line4
5|aa5|Line5

This User Gave Thanks to RudiC For This Post:
# 6  
Old 12-21-2018
Code:
sed 's/[0-9]\+\s*-\s*//'

This User Gave Thanks to nezabudka For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed and awk giving error ./sample.sh: line 13: sed: command not found

Hi, I am running a script sample.sh in bash environment .In the script i am using sed and awk commands which when executed individually from terminal they are getting executed normally but when i give these sed and awk commands in the script it is giving the below errors :- ./sample.sh: line... (12 Replies)
Discussion started by: satishmallidi
12 Replies

2. Shell Programming and Scripting

Trim sed output & assign to variable

Hi, I have the following command that parses an xml file to read a node <port>'s value. Hoever the output comes with spaces. My requirement is to trim the spaces around the value and assign to a variable. sed -n 's|<port>\(.*\)</port>|\1|p' ../cfg.xml How do I go about it? (6 Replies)
Discussion started by: sai2013
6 Replies

3. Shell Programming and Scripting

awk - trim white space from a field / variable

Hi, Consider the data (FS = |): 1| England |end 2| New Zealand |end 3|Australia|end 4| Some Made Up Country |end 5| West Indies|end I want the output to be (i.e. without the leading and trailing white space from $2) England New Zealand Australia Some Made Up Country West... (4 Replies)
Discussion started by: Storms
4 Replies

4. Shell Programming and Scripting

Help with awk trim

I am trying to trim spaces for the fixed width file starting from location 129 and of length 20. I am expecting only around 40 records that will have length greater than 9. But i am getting around 4000 records. Please help me correct the following. nawk '{if (a=length(gsub(/... (2 Replies)
Discussion started by: pinnacle
2 Replies

5. Shell Programming and Scripting

Sed or trim to remove non alphanumeric and alpha characters?

Hi All, I am new to Unix and trying to run some scripting on a linux box. I am trying to remove the non alphanumeric characters and alpha characters from the following line. <measResults>883250 869.898 86432.4 809875.22 804609 60023 59715 </measResults> Desired output is: 883250... (6 Replies)
Discussion started by: jackma
6 Replies

6. UNIX for Dummies Questions & Answers

Trim String in 3rd Column in Tab Delimited File...SED/PERL/AWK?

Hey Everybody, I am having much trouble figuring this out, as I am not really a programmer..:mad: Datafile.txt Column0 Column1 Column2 ABC DEF xxxGHI I am running using WGET on a cronjob to grab a datafile, but I need to cut the first three characters from... (6 Replies)
Discussion started by: rickdini
6 Replies

7. Shell Programming and Scripting

cutting fields and doing trim in awk

hi, I have this code: #!/usr/bin/ksh #SERVICES gzcat *SERVICES* | awk ' { SUBSCRIBERNUMBER=substr($0,1,20) SUBSCRIBERNUMBER=trim(SUBSCRIBERNUMBER) SERVICECODE=substr($0,22,61) SERVICECODE=trim(SERVICECODE) STARTDD=substr($0,63,72) STARTDD=trim(STARTDD) STARTDT=substr($0,74,81)... (1 Reply)
Discussion started by: naoseionome
1 Replies

8. Shell Programming and Scripting

Trim inside awk

Hi all, I am using qwk to parse the logfile. The code like awk ' { if($0 > " ") { MSISDN=substr($0,1,10) HOUR=substr($0,11,6); ID_SA_SOURCE=substr($0,17,18); ID_SA_DEST=substr($0,35,18); ... (3 Replies)
Discussion started by: subin_bala
3 Replies

9. Shell Programming and Scripting

Trim

Hello, I am passing a filename to a script to draw parameters from it. However, I want to use part of the filename as a parameter. The filename is transfer_ccf_3731_10.sh but I only need the 3731_10 part of it. Is this possible? Any help or suggestions would be appreciated! Regards, J. (4 Replies)
Discussion started by: JWilliams
4 Replies

10. Shell Programming and Scripting

Trim white spaces using awk

Hi, I have a CSV file with footer information as below. The third value is the number of records in the file. Sometimes it contains both leading and trailing white spaces which i want to trim using awk. C,FOOTER , 00000642 C,FOOTER , 00000707 C, FOOTER,... (2 Replies)
Discussion started by: mona
2 Replies
Login or Register to Ask a Question