Visit The New, Modern Unix Linux Community


Using sed to insert at position x and then every interval y for each line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using sed to insert at position x and then every interval y for each line
# 1  
Using sed to insert at position x and then every interval y for each line

Thanks to help from Don Cragun in post 302924174, I'm off and getting into trouble on my own (finally) with sed.

Here is my goal - insert
Code:
\\r\n

at the 60th character on each line and then every 76th character thereafter:

Input:
Code:
6F,00,72,00,20,00,73,00,69,00,74,00,20,00,61,00,6D,00,65,00,74,00,2C,00,20,00,63,00,6F,00,6E,00,73,00,65,00,63,00,74,00,65,00,74,00,75,00,72,00,2E,00,22,00,00,00;
22,00,54,00,68,00,65,00,20,00,71,00,75,00,69,00,63,00,6B,00,20,00,62,00,72,00,6F,00,77,00,6E,00,20,00,66,00,6F,00,78,00,5C,00,20,00,63,00,6F,00,6E,00,73,00,65,00,63,00,74,00,65,00,74,00,75,00,72,00,2E,00,22,00,00,00;

Output:
Code:
6F,00,72,00,20,00,73,00,69,00,74,00,20,00,61,00,6D,00,65,00,\
74,00,2C,00,20,00,63,00,6F,00,6E,00,73,00,65,00,63,00,74,00,65,00,74,00,75,\
00,72,00,2E,00,22,00,00,00;
22,00,54,00,68,00,65,00,20,00,71,00,75,00,69,00,63,00,6B,00,\
20,00,62,00,72,00,6F,00,77,00,6E,00,20,00,66,00,6F,00,78,00,5C,00,20,00,63,\
00,6F,00,6E,00,73,00,65,00,63,00,74,00,65,00,74,00,75,00,72,00,2E,00,22,00,\
00,00;

So I tried:
Code:
sed 's/./&\\\r\n/60 s/./&\\\r\n/76' input

but get a syntax error for char 16 being an unknown option to 's'.

This is puzzling to me as I'm able to successfully insert each 60 characters of every line with:
Code:
sed 's/./&\\\r\n/60' input

Shouldn't latter throw the same error?

Kind regads,

gus
# 2  
Quote:
Originally Posted by gusbrown
and then every 76th character thereafter:
According to your output it's every 78th character thereafter?

So I tried:
Quote:
Originally Posted by gusbrown
Code:
sed 's/./&\\\r\n/60 s/./&\\\r\n/76' input

but get a syntax error for char 16 being an unknown option to 's'.
If you carefully look at Don's sed command, you'll notice that the two "s" statements are delimited by a semicolon.

Quote:
Originally Posted by gusbrown
Code:
sed 's/./&\\\r\n/60' input

Shouldn't latter throw the same error?
No, because it's only a single "s" statement.

Try this work-around (you see it's obviously 60+78+78, thus using this approach you might need to add some more "s" statements if the lines are longer than the sample lines you posted in this thread):
Code:
sed 's/./&\\\r\n/60; s/./&\\\r\n/138; s/./&\\\r\n/216' input

---------- Post updated at 06:17 PM ---------- Previous update was at 06:11 PM ----------

I retract my words regarding 78 characters, however 78 were needed to produce the required output Smilie
This User Gave Thanks to junior-helper For This Post:
# 3  
Thank you for the pointers, had to go all the way to 606 but it works a charm!
# 4  
Print first 60 then strip them off
loop: print first 75 then strip them off
Code:
sed -n '
s/.\{60\}/&\\\r\n/;P;s/.*\n//
:L
s/.\{75\}/&\\\r\n/;P;s/.*\n//
tL
' input

---------- Post updated at 01:06 PM ---------- Previous update was at 12:42 PM ----------

The same, this time with 20 then 25 comma-separated fields:
Code:
sed -n '
s/[^,]*,/&\\\r\n/20;P;s/.*\n//
:L
s/[^,]*,/&\\\r\n/25;P;s/.*\n//
tL
' input

NB the \{count\} is faster than a //count repetition, but
s/[^,]*,/&\\\r\n/20
looks better than
s/\([^,]*,\)\{20\}/&\\\r\n/

Last edited by MadeInGermany; 11-07-2014 at 01:50 PM.. Reason: .*\n is easier
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #704
Difficulty: Medium
Under the same-origin policy, a web browser permits scripts contained in a first web page to access data in a second web page, but only if both web pages have the same origin.
True or False?

10 More Discussions You Might Find Interesting

1. Solaris

sed insert into line 1 via script

Hi I am trying to run a sed command within a script..unfortunately it wasn't written on Solaris so doesn't work. Can anyone help with the correct coding please? It is: sed -i '1i ROWID;ORDER_ID;JOB_NAME;ORDER_TABLE' ${OUTFILE} (4 Replies)
Discussion started by: Grueben
4 Replies

2. Shell Programming and Scripting

sed - How to insert line before the first blank line following a token

Hello. I have a config file (/etc/my_config_file) which may content : # # port for HTTP (descriptions, SOAP, media transfer) traffic port=8200 # network interfaces to serve, comma delimited network_interface=eth0 # set this to the directory you want scanned. # * if have multiple... (6 Replies)
Discussion started by: jcdole
6 Replies

3. Shell Programming and Scripting

Insert charactera in 1st position of specific lines using vi editor or sed command

Dear all, i am having text file like below surya rama ranga laxman rajesh reddy i want add string (OK) before a text from line 3 to 5 the result will be surya rama OK ranga OK laxman OK rajesh reddy (1 Reply)
Discussion started by: suryanarayana
1 Replies

4. Shell Programming and Scripting

Insert carriage return on the 10th char position of each line

Hi experts, Need your help on how to insert carriage return after the 10th char position of each line in a file and then add two blank spaces after the carriage return. Example: >cat test.txt testingline dummystring samplesample teststringline Expected output should be.. ... (2 Replies)
Discussion started by: brichigo
2 Replies

5. Shell Programming and Scripting

Insert @Line # - SED (non-GNU)

Just posted on another fellow's question using ed, but I wanted to know about doing it with sed on Unix. For example - I have a file of an unknown length, but I want to add a line after the shell declaration (Line 2). If possible, I'd like the example to be able to just substitute in a number and... (2 Replies)
Discussion started by: Vryali
2 Replies

6. AIX

insert a line with variables using sed

Hi, I have to insert a line having variables using sed. But the variables are not getting substituted within sed. Ex: n=2 sed $n' i\ hi' file This works. But the below code does not work. n=2 line=hello sed $n' i\ $line' file The above code inserts '$line' in the 2nd line of the... (9 Replies)
Discussion started by: sugan
9 Replies

7. Shell Programming and Scripting

sed insert text at particular line

I know that sed -n '12p' file will print line 12 but how might I insert text to a specified line? thanks (2 Replies)
Discussion started by: action_owl
2 Replies

8. Shell Programming and Scripting

search a line and insert string into specific at position

Hi, guys. I have one question: How can I search for a line with certain string in it and then insert a string into this line? For example: There is a file called shadow, the contents of it are below: ************************** ... yuanz:VIRADxMsadfDF/Q:0:0:50:7:::... (9 Replies)
Discussion started by: daikeyang
9 Replies

9. Shell Programming and Scripting

insert server name in position 1 on each line

Hello, It has been a long time since I have written unix code and I need to insert a variable into the first position of each line in a file. Below is an example of the script and the desired output file Here is my short script server="$(hostname)" df -kg | awk '{print $1, $2, $3, $4,... (3 Replies)
Discussion started by: rlc198842
3 Replies

10. Shell Programming and Scripting

sed - how to insert chars into a line

Hi I'm new to sed, and need to add characters into a specific location of a file, the fileds are tab seperated. text <tab> <tab> text <tab> text EOL I need to add more characters to the line to look like this: text <tab> <tab> newtext <tab> text <tab> text EOL Any ideas? (2 Replies)
Discussion started by: tangentviper
2 Replies

Featured Tech Videos