Help with pipes in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with pipes in a file
# 1  
Old 06-05-2007
Help with pipes in a file

I have to add pipes for particualr number of records in a file. For eg the file has 10 records and i need to add the "|" for records numbers 7 to 10 at particular positons as shown.

Quote:
90986003RW2007-05-309999-12-31
90986005RW2007-05-309999-12-31
I need to add pipes in each of these records at positions 9, 11,,21 as like below.

Quote:
90986003|RW|2007-05-30|9999-12-31
90986005|RW|2007-05-30|9999-12-31
Can some body let me know how i can acheive this.
# 2  
Old 06-05-2007
use vi

7,10s/RW/|RW|/
7,10s/9999/|9999/

save and quit
# 3  
Old 06-05-2007
thanks jgt.
# 4  
Old 06-05-2007
nawk -f ds.awk myFile.txt

ds.awk:
Code:
function setFieldsByWidth(   i,n,FWS,start,copyd0)
{

  copyd0 = $0
  n = split(FIELDWIDTHS,FWS)

  if (n == 1) {
    print "Warning: FIELDWIDTHS contains only one fie"
    print "Attempting to continue." > "/dev/stderr"
  }

  start = 1
  for (i=1; i <= n; i++) {
    $i = substr(copyd0,start,FWS[i])
    start = start + FWS[i]
  }
}
BEGIN {
  FIELDWIDTHS="8 2 10 9999"
  OFS="|"
}
FNR >=7 && FNR<=10 && !/^[  ]*$/ {
  saveDollarZero = $0 # if you want it later
  setFieldsByWidth()
  # now we can manipulate $0, NF and $1 .. $NF as we wish
  print $0
  next
}
1


Last edited by vgersh99; 06-05-2007 at 04:03 PM..
# 5  
Old 06-05-2007
Thanks Vgresh!!!! Can you help me with this please.


I have to add | at the end of the 8 th field in the below file and that too for all the reocrds in the file

Quote:
909860030116N03/13/2006
909860050407N03/13/2006
909860070427N03/13/2006
909860090431N03/13/2006
The out put should look like this:
Quote:
90986003|0116N03/13/2006
90986005|0407N03/13/2006
90986007|0427N03/13/2006
90986009|0431N03/13/2006
How i can do this. Please suggest.
# 6  
Old 06-05-2007
Code:
sed '7,10s/\(.\{8\}\)\(.\{2\}\)\(.\{10\}\)/\1|\2|\3|/' input_file

# 7  
Old 06-05-2007
Quote:
Originally Posted by dsravan
Can you help me with this please.
I have to add | at the end of the 8 th field in the below file and that too for all the reocrds in the file...
How i can do this. Please suggest.
Code:
sed 's/.\{8\}/&|/'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove pipes from end of file

Hello all, I have a file like this AA|| BB|| CC|| I want to remove the pipelines (||) from the last occurrance of them in file. that means after removal my file should be like this AA|| BB|| CC how can we achieve this? (2 Replies)
Discussion started by: nnani
2 Replies

2. Shell Programming and Scripting

Pipes with the < and >> meanings.

Hey, Well, we're starting scripting next week for my class, and I have my command done, but I don't actually understand what the meaning is, so I was just wondering if someone could "translate" this in to words so that I might be able to do this better... command1 | command2 | command3... (5 Replies)
Discussion started by: sso
5 Replies

3. Shell Programming and Scripting

How to combine these to pipes?

ls --color=always -laX | awk '{print $1, $3, $4, $2, $8}' |sort -k 1,1 -k 9,9r they work separately... but i don't know how to combine this to work. thx! (1 Reply)
Discussion started by: surreal7z
1 Replies

4. Programming

Pipes in C

Hello all, I am trying to learn more about programming Unix pipes in C. I have created a pipe that does od -bc < myfile | head Now, I am trying to create od -bc < myfile | head | wc Here is my code, and I know I might be off, thats why I am here so I can get some clarification. #include... (1 Reply)
Discussion started by: petrca
1 Replies

5. Shell Programming and Scripting

Remove pipes in file using SED

Hello, I am writing a script to remove multiples pipes (|) from a file. It needs to look like this data|data|data instead of data|||data||data||||data I have found the correct sed command to do this, but I need to do it in a loop and a for loop has not worked for me. It needs to be done on... (3 Replies)
Discussion started by: TL56
3 Replies

6. Shell Programming and Scripting

perl help with pipes and file handles (simple issue)

Hello, I have a program which opens a pipe for communication using file handle and forks 5 child processes. @waitlist = (1,2,3,4,5); foreach $item (@waitlist) { pipe *{$item},RETHAND; unless ($pid = fork()) { # Child process print RETHAND... (1 Reply)
Discussion started by: the_learner
1 Replies

7. Shell Programming and Scripting

doubt about pipes

can we use pipes to redirect the output of any command to grep ..... like i wanted to write this script about checking the online status of a certain user so ...can i send the output of who to grep directly using pipes... one way was this : who > temp grep $uname temp i was wondering if... (4 Replies)
Discussion started by: evergreen_cool
4 Replies

8. Programming

pipes inside

Extremely need to understand some aspects of pipes realization. The main question is in which memory are pipes placed? (13 Replies)
Discussion started by: pranki
13 Replies

9. Shell Programming and Scripting

cd using pipes

Hi, Can the cd command be invoked using pipes??? My actual question is slightly different. I am trying to run an executable from different folders and the path of these folders are obtained dynamically from the front end. Is there a way in which i can actually run the executable... (2 Replies)
Discussion started by: Sinbad
2 Replies

10. Filesystems, Disks and Memory

PIPEs and Named PIPEs (FIFO) Buffer size

Hello! How I can increase or decrease predefined pipe buffer size? System FreeBSD 4.9 and RedHat Linux 9.0 Thanks! (1 Reply)
Discussion started by: Jus
1 Replies
Login or Register to Ask a Question