sed on a one line file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed on a one line file
# 1  
Old 06-24-2011
Question sed on a one line file

Hello,

First please excuse me for my poor english, i am french. Thank you for reading this thread and potentially helping me.

I have a 300ko file of one line (without any \n)
I want to create a new flat file with \n every 592 characters

I tried with some sed commands, but i am not very skilled...
I first found out that in a regexp you can't use the {number} pattern with a number larger than 255, so i tried this

Code:
cat myfile.src | sed -e 's/.\{255\}.\{255\}.\{82\}/&\n/g' > myfile.tgt

but i only get a empty file...
If you see what's wrong, or if you have another idea... maybe a awk ? or another command i don't know...
Thank you
# 2  
Old 06-24-2011
use fold command

Code:
 
bash-3.00$ echo "123456789" | fold -w 3
123
456
789

In your case, it is

Code:
fold -w 592 myfile.tgt  > /tmp/newfile.tgt

---------- Post updated at 02:30 PM ---------- Previous update was at 02:19 PM ----------

nawk :
Code:
 
nawk '{for(i=1;i<length;i=i+592){ print substr($0,i,592)} }' input.txt > ouput.txt

while loop

Code:
while read -n592 line ; do echo $line; done < input.xt > output.txt

This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 06-24-2011
Quote:
Originally Posted by marcello_fr
I want to create a new flat file with \n every 592 characters

I tried with some sed commands, but i am not very skilled...
I first found out that in a regexp you can't use the {number} pattern with a number larger than 255, so i tried this

Code:
cat myfile.src | sed -e 's/.\{255\}.\{255\}.\{82\}/&\n/g' > myfile.tgt

but i only get a empty file...
If you see what's wrong, or if you have another idea... maybe a awk ? or another command i don't know...
Thank you
Just a small change to your command will do the job..By the way there is no need to use cat. Give the input file to sed as below..
Code:
sed -e 's/\(.\{255\}.\{255\}.\{82\}\)/&\n/g' myfile.src > myfile.tgt

This User Gave Thanks to michaelrozar17 For This Post:
# 4  
Old 06-24-2011
Computer

wow! i don't expected such quick and accurate answers, this unix.com is a great place Smilie

@itkamaraj : the command fold is exactly what i need, thank you very much ! it works very well Smilie

@michaelrozar17 i found the syntax with cat in the man sed, but indeed your syntax is simpler. i tried your suggestion with the \( ... \) but it does the same thing, i get an empty file. But i will use the "fold" command, so there is no real need to go further in this way.

Thank you very much for your replies !
# 5  
Old 06-24-2011
Wot sed version you are using..? It works fine for me with GNU Sed.
# 6  
Old 06-24-2011
I'm working on a server with AIX 5.3
Maybe it explains why the sed command doesn't work here...!
# 7  
Old 06-24-2011
I just got fold -c592 <bigfile >bigfile2 to work but I can't test it on AIX.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using sed to extract line of a file

Ok .. This is driving me nuts. I suppose I need another set of eyes. Example Data interface Vlan1 description xxxxxxxxxxxx ip address 192.168.1.1 255.255.255.252 no ip redirects no ip proxy-arp ip flow ingress ip pim sparse-mode load-interval 30 ! interface Vlan2 ... (5 Replies)
Discussion started by: popeye
5 Replies

2. 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

3. Shell Programming and Scripting

sed to replace a line with modified line in same file

i have few lines in a file... i am reading them in a while loop so a particular line is held is $line1.. consider a modified line is held in $line2.... i want to replace $line1 with $line2 in the same file... how to do it..? i have come up till the below code sed "s/$line1/$line2/g" tmpfile.sql... (5 Replies)
Discussion started by: vivek d r
5 Replies

4. Shell Programming and Scripting

copying a line from a file using sed

Hi All, I need to copy a specific line from a file to another file. lets suppose the line number 13 of a file when I am writing the line number explicitly.. its working fine sed -n '13p' afile > anotherfile but, when inside a script, i am getting the line number value inside a variable... (4 Replies)
Discussion started by: gotamp
4 Replies

5. Shell Programming and Scripting

Extract a number from a line in a file and sed in another copied file

Dear all, I am trying to extract a number from a line in one file (task 1), duplicate another file (task 2) and replace all instances of the strings 300, in duplicated with the extracted number (task 3). Here is what I have tried so far: for ((k=1;k<4;k++)); do temp=`sed -n "${k}p"... (2 Replies)
Discussion started by: mnaqvi
2 Replies

6. Shell Programming and Scripting

how to use sed for reading from a file, line by line?

I have a file, from where I need to extract some data. But, the condition is like following : The script will read line by line,while checking if any line starts with 'MN'. If found true, it looks for if the immediate line starts with 'PQ' or not. If its true then, extract few fields from that... (1 Reply)
Discussion started by: mady135
1 Replies

7. Shell Programming and Scripting

sed to read line by line and input into another file

I have two files. Fileone contains text string one text string two text string three Filetwo contains Name: Address: Summary: Name: Address: Summary: Name: Address: Summary: I would like to use sed to read each line of file one and put it at the end of the summary line of file... (3 Replies)
Discussion started by: dolacap
3 Replies

8. Shell Programming and Scripting

Sed grabbing the last line of a file

I can grab the first line w/ sed how do you grab the last line of a file? (2 Replies)
Discussion started by: xgringo
2 Replies

9. Shell Programming and Scripting

Replace a line in a file with sed

Hi, I am trying to write a script that will replace "PermitEmptyPasswords yes" with "PermitEmptyPasswords no". The following does not seem to work: - sed 's!/"PermitEmptyPasswords yes"/!/"PermitEmptyPasswords no"/!' Appreciate any ideas. Thanks (2 Replies)
Discussion started by: mtech3
2 Replies

10. Shell Programming and Scripting

delete line in file with sed

sed "/$titlesearch/d" movielist will delete any line in the file movielist that matches $titlesearch, but this is only a screen print correct ? how can I have sed to actually delete the line from the file so that it doesnt appear next time I open the file ? thanks Steffen (8 Replies)
Discussion started by: forever_49ers
8 Replies
Login or Register to Ask a Question