Solved: Sed to insert pipe separator

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

grinder182533

Thread Starter
Joined
Jun 1, 2009
Messages
72
Hi
My txt file consists of records with 6 numbers followed by 3 characters.
Is there a simple “sed” which will insert a | separator between the 6th and 7th position ?
Many thanks
 
Joined
Sep 12, 2003
Messages
20,583
Hi grinder182533,

Sed is not very amenable to position character replacement in a line, but the best way to do what you need is to combine the use of a bash shell script with the cut command for each line and use wc -l to count the lines along with nawk to assign them from a pipe (backquoted) to a variable name, then use $varname to dereference with expr in a count down loop with both head and tail to modify a temporary output file that at the end replaces the original file.

Work on it.

-- Tom
 

TerryNet

Terry
Moderator
Joined
Mar 23, 2005
Messages
80,906
My txt file consists of records with 6 numbers followed by 3 characters.
Do you mean 7 fields as in 1 2 3 4 5 6 abc?

Or do you mean one 9 character field as in 123456abc?
 
Joined
Sep 12, 2003
Messages
20,583
Hi grinder182533,

Here is an example shell script and file that demonstrates a solution to your problem. To use it, you will have to edit the input file in the script (assumed to be named - file) to the name of your input file. Needless to say, I consider this a quick hack and not a polished solution to your problem - however, it works!

The following shell script, named test.sh, assumes an input file (coincidentally named file) with the contents:
abcdefghijklmnopqrstuvwxyz
zyxwvutsrqponmlkjihgfedcba
abcdefghijklmnopqrstuvwxyz
zyxwvutsrqponmlkjihgfedcba

The code for the script test.sh is:
Code:
#!/bin/bash -x
#test file content lines:
#abcdefghijklmnopqrstuvwxyz
#zyxwvutsrqponmlkjihgfedcba
#abcdefghijklmnopqrstuvwxyz
#zyxwvutsrqponmlkjihgfedcba
cp file testfile
nl=`wc -l testfile | nawk '{ print $1}'`
nlm1=`expr $nl - 1`
i=$nl
while [ $i -gt 0 ] ; do
  # get next line in file
  tmp0=`cat testfile | head -1`
  echo $tmp0 > tmp
  cat testfile | tail -$nlm1 > testfile0
  cat testfile0
  left=`cut -c 1-6 tmp`
  right=`cut -c 7- tmp`
  new=$left"|"$right
  echo $new >> newtmp
  if [ $nlm1 -gt 0 ]
  then test0=`wc -l testfile0 | nawk '{ print $1 }'`
       cp testfile0 testfile
       nlm1=`expr $nlm1 - 1`
       i =`expr $i - 1`
  else rm testfile testfile0 tmp
       mv newtmp file 
       exit 1
  fi
done
Note: the first line uses the -x for debugging the script. The script will rewrite the input file named file with the desired result - i.e. a '|' character between the 6th and 7th column position in the file. Also, note: the script was not coded to deal with blank lines.

-- Tom
 

grinder182533

Thread Starter
Joined
Jun 1, 2009
Messages
72
Hi Ted and Terry.
Sorry for the delay in response : I was ill then on holiday.
I have solved my problem and am getting to grips with sed, but it isn't easy !
Thanks again.
 

grinder182533

Thread Starter
Joined
Jun 1, 2009
Messages
72
Indeed it is : I was looking at something else I had to do which was similar to the original post.
Thanks again for your help Tom.
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Members online

Top