Solved

Help using sed or awk for file edit

Posted on 2007-04-02
8
417 Views
Last Modified: 2010-04-21
Can I ask for some help editting a file using sed or awk?

I have a pipe delimited file with 30 fields per record.

The 30th field contains numbers.

I want to find all the 12 digit numbers that start with a 7, and change them to eleven digit numbers.  Basically I need to remove the leading 7 from the 12 digit numbers so I am left with a 11 digit number in that field.
0
Comment
Question by:roone
  • 5
  • 3
8 Comments
 
LVL 45

Expert Comment

by:Kdo
ID: 18837937
Hello roone,

There are several ways to do this.  The easiest sed solution is to assume that the last field is being edited.

Change the ',' to the correct separator, if necessary.  This solution requires that there are no null fields (there are no consecutive separators).


sed 's/,7[0-9]\{11\}$/,&/; s/,,7/,/' myfile

Good Luck,
Kent
0
 

Author Comment

by:roone
ID: 18838010
The records are separated by |

OK.
Thanks.
0
 
LVL 45

Expert Comment

by:Kdo
ID: 18838033

Make sure to escape the '|' character.   :)

sed 's/\|7[0-9]\{11\}$/\|&/; s/\|\|7/\|/' myfile


0
 

Author Comment

by:roone
ID: 18838078
# cat testsed
s/|7[0-9]\{11\}$/|&/; s/||7/|/
#

I made this sed file.  I used it on a test file, and it just removed the numbers starting with 7.

Can you break down the actual expression for me?

I understand the first part "search for a pipe followed by a seven and then 11 other digits"
s/|7[0-9]\{11\}
, right?

But I get lost after the $
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:roone
ID: 18838087
Oh, wait.  There are consecutive separators.

But I am definately only touching the last field.

Can't I say, grab the 30th field and then do this substitution?

0
 

Author Comment

by:roone
ID: 18838192
# cat trythis
s/\(7\)\([0-9]\{11\}\)/\2/g


This worked for me.

Thanks.


0
 
LVL 45

Accepted Solution

by:
Kdo earned 50 total points
ID: 18838703
Hi roone,

You can certainly grab the 30th something.  The script that I provided grabbed the last field, which happens to be the 30th.  :)

I don't know your data very well, but your script doesn't care where the 7........... entry is.  And will change ALL occurences, not just parameter 30.


Kent
0
 

Author Comment

by:roone
ID: 18839151
Understood.  Thanks for the help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Unix Question 19 45
What is the meaning of mount pont? with respect to windows file server. 7 57
Solaris 4.1.3 cloning and booting 13 72
Martian Packets Unix 5 31
This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now