The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Remove pipes in file using SED TL56 Shell Programming and Scripting 3 10-17-2007 12:03 PM
perl help with pipes and file handles (simple issue) the_learner Shell Programming and Scripting 1 05-06-2007 06:34 AM
doubt about pipes evergreen_cool Shell Programming and Scripting 4 09-10-2005 02:05 AM
cd using pipes Sinbad Shell Programming and Scripting 2 09-09-2004 11:05 AM
PIPEs and Named PIPEs (FIFO) Buffer size Jus Filesystems, Disks and Memory 1 08-20-2004 11:14 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-05-2007
dsravan dsravan is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 190
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 (permalink)  
Old 06-05-2007
jgt jgt is offline
Registered User
  
 

Join Date: Apr 2007
Location: 44.21.48N 80.50.15W
Posts: 455
use vi

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

save and quit
  #3 (permalink)  
Old 06-05-2007
dsravan dsravan is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 190
thanks jgt.
  #4 (permalink)  
Old 06-05-2007
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
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 (permalink)  
Old 06-05-2007
dsravan dsravan is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 190
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 (permalink)  
Old 06-05-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
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\}/&|/'

  #7 (permalink)  
Old 06-05-2007
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
Quote:
Originally Posted by dsravan
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



The out put should look like this:


How i can do this. Please suggest.
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 9999"
  OFS="|"
}
!/^[  ]*$/ {
  saveDollarZero = $0 # if you want it later
  setFieldsByWidth()
  # now we can manipulate $0, NF and $1 .. $NF as we wish
  print $0
  next
}
1

or Shell_Life's 'sed'
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:37 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0