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
how to append spaces(say 10 spaces) at the end of each line based on the length of th prathima UNIX for Dummies Questions & Answers 17 01-28-2009 04:10 PM
Relace text based on position dncs Shell Programming and Scripting 1 05-28-2008 09:39 PM
Spliting the line based on position. senthil_is Shell Programming and Scripting 1 09-26-2007 02:32 AM
Sorting a flat file based on multiple colums(using character position) cucubird Shell Programming and Scripting 8 07-25-2006 01:47 AM
Append a field to the end of each line of a file based on searching another file. ultimate Shell Programming and Scripting 2 03-29-2005 11:21 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 03-18-2008
ashikin_8119 ashikin_8119 is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 10
Append line based on fixed position

Hi all;

I'm having headache on append one line to another based on the fix position.Hope u guys can help.

All i need to do is append the line that start with '3' to a line which start with '1' and the position for line 3 that i need to append is 22.

The original file look like this:

1CSTN0012008010312
2A45021906080120070303131709000017600
2S45333282010520020065411701000017600
2Z30092614090520020065635811000026400
2Z21094701020220020065649305000019800
2S35343116010420020065650001000008800
2S35344198010120020065651208000006600
2S45333998010620020065702107000008800
2S35344254010220020065704708000013200
2D93688321010220020065708106000006600
2D93686199010420020065710405000009900
2K40052945050820020065714210000004400
2U30030159060220070304010210000013200
30000012000152900

I need to rearrange the file to make it look like this:

1CSTN0012008010312 30000012000152900
2A45021906080120070303131709000017600
2S45333282010520020065411701000017600
2Z30092614090520020065635811000026400
2Z21094701020220020065649305000019800
2S35343116010420020065650001000008800
2S35344198010120020065651208000006600
2S45333998010620020065702107000008800
2S35344254010220020065704708000013200
2D93688321010220020065708106000006600
2D93686199010420020065710405000009900
2K40052945050820020065714210000004400
2U30030159060220070304010210000013200

For your information the length for line that start with '1' is not fixed,it can be less than 21 character.

I'm having a problem when the length of the line (that starts with '0') is 21.
I try to use if ... else but got error msg. This is the script that i used

cat CSTN001.dat | awk '
/^1/ { line0 = $0}
{
if ( length(line0) < 21 )

FS=" "; for (i=1; i<(21-(length($0))); i++) FS=FS " ";
/^1/ { sub(/1/, "0") }
/^3/ { line1 = $0 }
/^0/ { $NF = ($NF FS line1) }
{ x[FNR] = $0 }
END{
for (i=1; i<=FNR; i++)
if (x[i] ~ /^0/)
print x[i]
for (j=1; j<=FNR; j++)
if (x[j] !~ /^[03]/)
print x[j]
}

else
/^1/ { sub(/1/, "0") }
/^3/ { line1 = $0 }
{ x[FNR] = $0 }
END{
for (i=1; i<=FNR; i++)
if (x[i] ~ /^0/)
print x[i] line1
for (j=1; j<=FNR; j++)
if (x[j] !~ /^[03]/)
print x[j]
}
}
' > CSTN001.new


Someone pls help...Thank you in advance
  #2 (permalink)  
Old 03-19-2008
faltooweb faltooweb is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 11
Append line based on fixed position

awk '
/^1/ { s=$0 }
/^3/ {
if ( s == "" ) next
printf "%-21s%s\n",s,$0
s=""
}
' $INFILE

-Ramesh
  #3 (permalink)  
Old 03-19-2008
ashikin_8119 ashikin_8119 is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 10
Thanks Ramesh

It works perfectly!!...really appreciate your help
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 04:18 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