Insert varying length spaces between words


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert varying length spaces between words
# 1  
Old 06-24-2011
Insert varying length spaces between words

Hey all,

Fist post, so be kind... I have written an expect script which logs into a terminal and gathers several screens of information. Unfortunately the log file gives me all the special escape and control characters from the terminal. I am hoping to use a combination of shell scripting, sed, and awk to clean it up and retain the spacing. I have cleaned the output to the point where it is in the following format:

01A03;10HANALOG LINE;32HTN793;40HB;41H;43H000017;54H01;57H02;60H03;63H04;66H05;69H06;72H07;75H08
;54H09;57H10;60H11;63H12;66H13;69H14;72H15;75H16
;54H17;57H18;60H19;63H20;66H21;69H22;72H23;75H24

The ;numberH portion of the line indicates where the cursor should start writing the next portion of text. So what I want to do is insert the correct number of spaces where this information is.

Here is the way I figure the logic should go. Each of the values between the ; and H need to have 1 subtracted from them because the value is actually the position of the first character. Then, starting from the end of the line, the value needs to have the previous value and the number of characters subtracted, in order to find the number of spaces between the words. It should ultimately look like this:

Code:
01A03    ANALOG LINE              TN793B  000017     01 02 03 04 05 06 07 08
                                                     09 10 11 12 13 14 15 16
                                                     17 18 19 20 21 22 23 24

Hope this all makes sense, Thanks in advance for your help!
# 2  
Old 06-24-2011
It's from zsh command line, but it's better, of course, put it in a script:
Code:
% perl -ne '
@F = split /(;\d+H[^;]*)/;
$cur = 0;
for (@F) {
  /(;(\d+)H)?([^;]*)/;
  $s = " " x ($2-$cur-1) . $3;
  $cur += length $s;
  print $s;
}
' testfile
01A03    ANALOG LINE           TN793   B  000017     01 02 03 04 05 06 07 08
                                                     09 10 11 12 13 14 15 16
                                                     17 18 19 20 21 22 23 24

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Vi - insert a tab between words?

I have several lines in a file that I want to replace a space with a tab. For example: 111047 Julie Jones email@email.com 111047 Julie Jones email@email.com I want to replace the space after the word "jones" with a tab. How do I achieve that in Vi? Please assist. Thanks! (5 Replies)
Discussion started by: onlinelearner02
5 Replies

2. Shell Programming and Scripting

Concatenating words without spaces.

Hi All, I have written a C program to solve this problem but I am eager to know whether the same output can be obtained using sed or awk? This is the input: star ferry computer symbol prime time This is the output: starferry ferrycomputer computersymbol symbolprime primetime (7 Replies)
Discussion started by: shoaibjameel123
7 Replies

3. UNIX for Dummies Questions & Answers

Sorting words based on length

i need to write a bash script that recive a list of varuables kaka pele ronaldo beckham zidane messi rivaldo gerrard platini i need the program to print the longest word of the list. word in the output appears on a separate line and word order in the output is in the order Llachsicografi costs.... (1 Reply)
Discussion started by: yairpg
1 Replies

4. UNIX for Dummies Questions & Answers

Display all the words whose length is equal to the longest word in the text

Hi Guys, I was going some trial and error to see if I can find the longest word in a text. I was using Pipes because they are easier to use in this case. I was stuck on this for a while so I thought I'll get some help with it. I tried this code to separate all the words in a text in... (4 Replies)
Discussion started by: bawse.c
4 Replies

5. Shell Programming and Scripting

Delete words greater than a specific length

HI All, I have a file with contents like this: apple computer terminal applecomputernetworkrouterterminalrouter network router applecomputernetworkrouterterminalrouter I want to remove all lines with length greater than "18 alphabets". Hence, my output should be: apple computer... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

6. Shell Programming and Scripting

Awk: Searching for length of words between slash character

Dear UNIX Community, I have a set of file paths like the one below: \\folder name \ folder1 \ folder2 \ folder3 \ folder4 \\folder name \ very long folder name \ even longer name I would like to find the length of the characters (including space) between the \'s. However, I want... (6 Replies)
Discussion started by: vnayak
6 Replies

7. Shell Programming and Scripting

Help with extracting words from fixed length files

I am very new to scripting and need to write a script that will extract the account number from a line that begins with HDR. For example, the file is as follows HDR2010072600300405505100726 00300405505 LBJ FREEWAY DALLAS TELEGRAPH ... (9 Replies)
Discussion started by: bds052189
9 Replies

8. Shell Programming and Scripting

Insert space between two words

Hi, I need to insert space between words on my output in UNIX other than the single space given by the space bar on my keyboard, e.g when are you going. (There should be 4 spaces between each of these words) rather than when are you going Can anyone help me with... (3 Replies)
Discussion started by: divroro12
3 Replies

9. UNIX for Dummies Questions & Answers

how to append spaces(say 10 spaces) at the end of each line based on the length of th

Hi, I have a problem where I need to append few spaces(say 10 spaces) for each line in a file whose length is say(100 chars) and others leave as it is. I tried to find the length of each line and then if the length is say 100 chars then tried to write those lines into another file and use a sed... (17 Replies)
Discussion started by: prathima
17 Replies

10. Shell Programming and Scripting

Retaining spaces between words

Retaining Spaces within a word -------------------------------------------------------------------------------- Hi Experts, I have a 2 GB flat file which have unicode field, some of them are blanks and its size is 4000 character. In the existing system SED command removes the spaces.... (7 Replies)
Discussion started by: RcR
7 Replies
Login or Register to Ask a Question