sed to have defined positionning on line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed to have defined positionning on line
# 1  
Old 04-10-2002
Question sed to have defined positionning on line

Hi,
I'd like to know how to use sed to position my output to a defined location on a line.

Here is my example:
A: filename.txt => line x : comment fromA
B: very longfilename.txt => line xyzabd : commentfromB

and I'd like to have as output from sed :

A: filename.txt................=> line x................: comment fromA
B: very longfilename.txt....=> line xyzabd........: commentfromB

In fact, i need to know how to rearrange and get aligned columns whatever my A and B internal fields are ?

Thanks a lot.....



Smilie Smilie Smilie
# 2  
Old 04-10-2002
I would suggest awk as a reformatter. There are many different approaches to identifying and isolating the various pieces of a line. In the following, awk uses the "=>" and the colon that occurs beyond that to identify segments of the line, then prints the line with an exact format.

Those widths on the print command are minimum column widths. If a line segment comes in that exceeds those minimums, the longer segments will be accommodated, thus pushing the columns out of alignment for that one line. We could easily truncate instead, but we don't want that. I kept the overall width to fit in 80 columns, but an output line can be up to about 2400 characters.

I did not do the dot-fill at this time. I did not check for bad data. If a line is missing the "=>" for example, the entire line would be seen as segment 1.
Code:
#!/bin/sh
awk '{
FS="=>"
part1=$1
$0=$2
FS=":"
printf "%-28s=>%-14s:%-34s\n",part1,$1,$2
}' myfile > myNEWfile
exit 0

Jimbo
# 3  
Old 04-11-2002
Thanks Jimbo,
I'm going to try asap.
Have a nice day Smilie Smilie Smilie
# 4  
Old 04-11-2002
I still receive an error message :

awk: can't set $0
record number 1

here is a right copy of my original line :
src**>...hwi_adc.....=>...Line...332:..../*..comments.....*/
cin**>...hwi_resource_manager.....=>...Line...33333:..../*..comments.....*/

(of course don't take care of ......)

And my goal is to get:
src**>...hwi_adc........................=>...Line...332......./*..comments.....*/
cin**>...hwi_resource_manager.....=>...Line...33333..../*..comments.....*/

Any idea ? Smilie
# 5  
Old 04-11-2002
Hmmm ...
I'm going to guess, for now, that your awk does not like making changes to the current buffer line, either entire line ($0) or even individual fields on the line like $1. Bummer, because I use that a lot on HP-UX. What is your platform?

OK, following is a solution that does not modify the buffer. It first identifies where in the line the => and the first /* are. It then generates pad1 and pad2 to be the required # dots to force the => to col 35 and the /* to col 57 (35+22). If an input line is already beyond these, nothing will get truncated, pad1 and pad2 will just be null for these.

Some platforms do not handle the quoted awk program spanning multiple lines, but I don't think you have that issue. If so, we can put line continuations and semicolons, or just bring in the awk code with the -f option.
Code:
#!/bin/sh
awk '{
aloc=index($0,"=>")
cloc=index($0,"/*")
pad1=substr("....................",1,35-aloc)
pad1len=length(pad1)
pad2=substr("....................",1,57-cloc-pad1len)
print substr($0,1,aloc-1) pad1 \
      substr($0,aloc,cloc-aloc) pad2 \
      substr($0,cloc)
}' myfile > myNEWfile
exit 0


Last edited by Jimbo; 04-11-2002 at 03:33 PM..
Jimbo
# 6  
Old 04-23-2002
Hi Jimbo,
sorry for the delay but I'm just back from vacation....some fresh air will help to think....
My platform is Sun Solaris OS 5.5.1 on Ultra 5.

Your solution does not work fine. Columns are not aligned yet.
Here is a cut and paste of my screen:


cin**> sk_5457.h => Line 1270 /*--- approach, each i/o mapped on the TPU must use the specific TPU ---*/
cin**> filter.c => Line 36 /* Lag_Filter_Approx_W -h- */
cin**> filter.c => Line 272 CARDINAL Lag_Filter_Approx_W (register CARDINAL NewValue,
cin**> filter.c => Line 297 } /*** End of Lag_Filter_Approx_W ***/
cin**> filter.h => Line 35 /* Lag_Filter_Approx_W */
cin**> filter.h => Line 87 CARDINAL Lag_Filter_Approx_W (register CARDINAL NewValue,
cin**> filter.h => Line 104 /* - Create Lag_Filter_Approx_W function to round up to the next */
cin**> h_qadc64.c => Line 47 /*--- This logic is called from the appropriate 58X event ---*/
cin**> v_wdg.c => Line 623 /*** appropriate EEPROM or ETC received buffer. ***/

and here is what I would expect as a result :

cin **> sk_5457.h ...........=> Line 1270 .................:........../*--- approach, each i/o mapped on the TPU must use the specific TPU ---*/
cin **> filter.c ...................=> Line 36 .....................:........../* Lag_Filter_Approx_W -h- */
cin **> filter.c ...................=> Line 272 ...................:..........CARDINAL Lag_Filter_Approx_W (register CARDINAL NewValue,
cin **> filter.c ...................=> Line 297 ...................:..........} /*** End of Lag_Filter_Approx_W ***/
cin **> filter.h ...................=> Line 35 .....................:........../* Lag_Filter_Approx_W */
cin **> filter.h ...................=> Line 87 .....................:..........CARDINAL Lag_Filter_Approx_W (register CARDINAL NewValue,
cin **> filter.h ...................=> Line 104 ...................:........../* - Create Lag_Filter_Approx_W function to round up to the next */
cin **> h_qadc64.c ........=> Line 47 ......................:........../*--- This logic is called from the appropriate 58X event ---*/
cin **> v_wdg.c ...............=> Line 623 ...................:.........../*** appropriate EEPROM or ETC received buffer. ***/

Do you see a solution ?
I was using the following awk but I get a problem when a "=" is part of left side....and I get only one sort on first "=>".


awk '{
FS="="
printf "%-35s=%s\n",$1,$2
}' /tmp/filea$$

In fact, the three missing columns to generate are :
at "=>" , at ":" and at start of last line (after the line number) which can be a comment or c code.

How do you concatenate a line with awk ? I was using something like :

{ for (i=6;i<=NF;i++)
part=part + $i" " }

but with no good result !

Thanks again,
homefp
# 7  
Old 04-23-2002
homefp, I just ran my awk solution against the data you just posted, and it worked perfectly for me. What result do you get?

I am at a 3-day intense seminar, so I will have no significant time nor brain power to spare for next 2-3 days.
Jimbo
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

selection particular number of line from a bunch by user defined limits.

hello i am having a file having a matrix as the following 4.1 5.5 6.55 7.2 8.2 1.002 i am having around 1 lakh rows, now i need a program in which i show give min x and min y and min z values and as well as max x max y max z, the values between these minimun and maximum values should be... (1 Reply)
Discussion started by: charan pattabhi
1 Replies

2. Shell Programming and Scripting

Sed/grep: check if line exists, if not add line?

Hello, I'm trying to figure out how to speed up the following as I want to use multiple commands to search thousands of files. is there a way to speed things up? Example I want to search a bunch of files for a specific line, if this line already exists do nothing, if it doesn't exist add it... (4 Replies)
Discussion started by: f77hack
4 Replies

3. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

4. Shell Programming and Scripting

sed and awk giving error ./sample.sh: line 13: sed: command not found

Hi, I am running a script sample.sh in bash environment .In the script i am using sed and awk commands which when executed individually from terminal they are getting executed normally but when i give these sed and awk commands in the script it is giving the below errors :- ./sample.sh: line... (12 Replies)
Discussion started by: satishmallidi
12 Replies

5. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

6. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

7. Shell Programming and Scripting

Sed Comparing Parenthesized Values In Previous Line To Current Line

I am trying to delete lines in archived Apache httpd logs Each line has the pattern: <ip-address> - - <date-time> <document-request-URL> <http-response> <size-of-req'd-doc> <referring-document-URL> This pattern is shown in the example of 6 lines from the log in the code box below. These 6... (1 Reply)
Discussion started by: Proteomist
1 Replies

8. Shell Programming and Scripting

Print the 2nd line everytime after defined pattern is found.

Hi, I have a text file similar to the example below and I want to print the second line every time after the "--------------------------" pattern is found. The pattern is a fixed length of - characters. Example of input; 1 -------------------------- 2 3 39184018234 4 ... (10 Replies)
Discussion started by: lewk
10 Replies

9. Shell Programming and Scripting

Sed or Grep to delete line containing patter plus extra line

I'm new to using sed and grep commands, but have found them extremely useful. However I am having a hard time figuring this one out: Delete every line containing the word CEN and the next line as well. ie. test.txt blue 324 CEN green red blue 324 CEN green red blue to produce:... (2 Replies)
Discussion started by: rocketman88
2 Replies

10. Shell Programming and Scripting

sed/awk to insert comment at defined line number

Hi there, may someone easily help me on this : I want to insert a text in a specific line number like : linenumb2start=`cat memory_map.dld | nl -ba | egrep -i "label" | cut -f1` line2insert=`expr $linenumb2start + 2` and now I need to replace something like {} with {comment} at... (8 Replies)
Discussion started by: homefp
8 Replies
Login or Register to Ask a Question