Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Help using sed or awk for file edit

Posted on 2007-04-02
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.
Question by:roone
  • 5
  • 3
LVL 45

Expert Comment

by:Kent Olsen
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,

Author Comment

ID: 18838010
The records are separated by |

LVL 45

Expert Comment

by:Kent Olsen
ID: 18838033

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

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Author Comment

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"
, right?

But I get lost after the $

Author Comment

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?


Author Comment

ID: 18838192
# cat trythis

This worked for me.


LVL 45

Accepted Solution

Kent Olsen 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.


Author Comment

ID: 18839151
Understood.  Thanks for the help.

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machineā€¦
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consolā€¦
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.:
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

840 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