Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cutting/Replacing characters at the end of a field 2

Status
Not open for further replies.

johngiggs

Technical User
Oct 30, 2002
492
US
How do I go about replacing/deleting the characters "2003" if they exist in field 1? The following cut command works assuming that all lines have 2003 as the 9th - 12th characters.

cut -c1-8,13-70 file

and

sed 's/2003 / /g'

Also works because there are 5 spaces between fields 1 and 2.

What is the best sed solution for performing this task?

Sample Input

Name Description
========================================================
tuesdayb2003 Tuesday wk1
tuesdays2003 2003 Tuesdays: Tuesdays w/oholiday
tuesfrid2003 2003 Tuesfrid: Tuesdays thru Fridays w/o

Desired Output

Name Description
========================================================
tuesdayb Tuesday wk1
tuesdays 2003 Tuesdays: Tuesdays w/oholiday
tuesfrid 2003 Tuesfrid: Tuesdays thru Fridays w/o

Any help would be greatly appreciated.

Thanks,

John
 
Actually, I only want to delete/cut the string "2003" if it exists and they are the last characters in the first field.

Thanks,

John
 
Assuming field 1 ends in a space and 2003 is always at the end of it (untested)

sed 's/^\(.*\)2003 /\1 /'

CaKiwi

"I love mankind, it's people I can't stand" - Linus Van Pelt
 
sed -e '/2003/s/^\([^ ][^ ]*\)2003\(.*\)/\1\2/g' file.txt

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
If you the number of spaces between fields does'nt matter:
Code:
awk '$1~/2003$/{$1=substr($1,1,length($1)-4)}
{print}' /path/to/inputfile >/path/to/outputfile
 
Thanks, vlad!! That did the trick. Can you please explain this portion of the command:

/^\([^ ][^ ]*\)2003\(.*\)/\1\2/


Thanks,

John
 
/^\([^ ][^ ]*\)2003\(.*\)/\1\2/

/ / <-- pattern to match for substitution.

^ <-- beginning of the line

\( \) <-- FIRST referencable expression

2003 <-- string '2003'

[^ ][^ ]* <-- ANY character BUT space
repeated more than ONCE
\( \) <-- SECOND referencable expression
.* <-- any number of ANY character


/ / <-- replacment for the
preceeding 'match'
\1\2 <-- subst with back-reference
expression ONE folowed by the
back-reference expression TWO


I'm not sure if that's the best way of explaining it, but...... What's taking place here is 'tagging' regexp matching and BACK-REFERENCING them in the 'replacement portion of the 'substitute command'. You can think of it as:

ANYTHING_but_space followed_by_2003 the_rest_of_the_line

what you need to get is:

ANYTHING_but_space the_rest_of_the_line

hope it helps



vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanks, Vlad!! [thumbsup] That was a good explanation. I understood what was happening, but I didn't fully understand the syntax that was used.

PHV, your solution works well also. Thanks for your help.

John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top