Need shell script for inserting New line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need shell script for inserting New line
# 1  
Old 04-20-2010
Need shell script for inserting New line

Hi All,

We have standard programming that creates large files. This file is very big. It contains header record,details records and Trailer record. Details record are of 1024 bytes(Each detail record contains summary(512 bytes) and detail(512 bytes) record).

My user wants to insert new line such that each detail record(1024) bytes is to cut into 2 lines of 512 bytes.

Please advise on this.

Thanks.
# 2  
Old 04-20-2010
use cut -c 1-512 and cut -c 513-

is psuedo code

Code:
while read a
if (substr(a,1,1)=h echo $a
if substr(a,1,1) = d
   echo `cut -c 1-512`
   echo `cut -c 513-`
fi
if substr(a,1,1)=t echo $a
done

# 3  
Old 04-20-2010
Thanks for your reply. I am new to shell scripting. We dont have specific header,details and trailer records. We assume first record is header and last 2 records are tailer and number of records and In between all detail files.
can you explain how to call file ,how to write the script and how to process detail records.
I understood main logic
echo `cut -c 1-512` echo `cut -c 513-`
Thanks for your Quick response jgt.
# 4  
Old 04-20-2010
Code:
#!/bin/ksh
read a                    
echo $a                   
while read a              
do                        
echo `echo $a|cut -c1-512` 
b=`echo  $a|cut -c 513-`
if [ "a$b" != "a" ]
then
  echo $b
fi
done

Run as
Code:
this.script <data_file >output_file

# 5  
Old 04-20-2010
Thank you very much jgt. I let you know once it is done.
# 6  
Old 04-20-2010
Another option is to use sed and awk. Assuming, file1 content is like the
following:

Code:
host1> cat file1
header
summary1detail1
summary2detail2
summary3detail3
trailer1
trailer2

You can do the following to achieve something like you are looking for:

Code:
host1>cat file1|sed '1d;$d'|sed '$d'|awk '{print substr($0, 1, 8); print substr($0, 9, 7)}'
summary1
detail1
summary2
detail2
summary3
detail3

Here the 'sed' command is used to get rid of the header and trailer and 'awk' is
used to separate the summary and detail part of each record.
'1d;$d' means delete the first and last line.
$0 in awk stands for each line read from input.

Hope this helps...
# 7  
Old 04-20-2010
No UUoC please Smilie
Code:
# cat file1
header
summary1detail1
summary2detail2
summary3detail3
trailer1
trailer2

# sed '1d;$d;s/\(.\{8\}\)\(.*\)/\1\
\2/' file1
summary1
detail1
summary2
detail2
summary3
detail3
trailer1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Inserting shell script input data automatically from a text file

Dear experts, I am new to linux programming. I have a shell script which i should run it on all my samples. I only define input and out put for this script. The inputs are 3 numbers(coordination numbers) which are available in a series of text file. Since i have a lots of samples, it takes a... (5 Replies)
Discussion started by: mohamadreza
5 Replies

2. UNIX for Dummies Questions & Answers

Inserting new line

Hi all, I want ot print a header with new line in the end. I am using echo like echo "this is header" I want output like This is header $ I tried -e also but it's not working. echo - e Can anyone help please? (6 Replies)
Discussion started by: dsa
6 Replies

3. Shell Programming and Scripting

Need help on inserting data from text file to excel using shell script

Hi, Please help me on this. I want to insert data from text file to excel using shell script nawk -v r=4 -v c=4 -v val=$a -F, 'BEGIN{OFS=","}; NR != r; NR == r {$c = val; print}' "file.csv" I used above one to insert $a value in 4th row, 4th column in an excel file.csv and it... (3 Replies)
Discussion started by: suman.frnz
3 Replies

4. Shell Programming and Scripting

Commenting a specific line and inserting a new line after commented line.

Hello All, I have following file contents cat file #line=aaaaaa #line=bbbbbb #line=cccccc #line=dddddd line=eeeeee #comment=11111 #comment=22222 #comment=33333 #comment=44444 comment=55555 Testing script Good Luck! I would like to comment line line=eeeeee and insert a new line... (19 Replies)
Discussion started by: manishdivs
19 Replies

5. Shell Programming and Scripting

script for inserting line at specific place in file

I use zentyal for my server admin, which is great but zentyal auto-generates config file on boot and hence overwrites any changes made directly to config files. In order to allow multiple user access to a MS ACCESS database, I need to customise the smb.conf file and add the following line to the... (9 Replies)
Discussion started by: barrydocks
9 Replies

6. Shell Programming and Scripting

Help with inserting a line

Hello, I am new to this forum. I have a question in Unix shell scripting. Here is my requirement I have 1000 files and I need to insert a header with one line at the top of each of the 1000 files. Please let me know if you have any solution for this Thanks in advance. Raj (4 Replies)
Discussion started by: trajashekar
4 Replies

7. Shell Programming and Scripting

Inserting a line in a file after every alternate line

Friends , I have a large file and i need to insert a line after every line.I am actually unaware how to do it.Any help appreciated. My File control station *ATM* , qread $OSS.Jul13A.FI01 interval 1 intcount 1 control station *ATM* , qread $OSS.Jul13A.FI02 interval 1 intcount... (4 Replies)
Discussion started by: appu2176
4 Replies

8. UNIX for Advanced & Expert Users

Inserting a line before the line which matches the patter

Hi Is there any command where we can insert a line "2|||" before every line starting with "3|" my input is as follows 1|ETG|12345 3|79.58|||GBP|| 1|ETG|12345 3|79.58|||GBP|| 1|ETG|12345 2|EN_GB||Electrogalvanize 0.5 m2 ( Renault ) 1|ETG|12345 3|88.51|||GBP|| desired output... (10 Replies)
Discussion started by: laxmi131
10 Replies

9. Shell Programming and Scripting

Inserting a new line

I have a file where each line starts with a "update" statement. I want to insert a new line, after each line, with the words "go" in the new line. Can someone help me out with this? (1 Reply)
Discussion started by: ssmallya
1 Replies

10. UNIX for Dummies Questions & Answers

inserting line??

guys can anyone tel me how to insert a line in a file(bash) using line number i wanna insert the line using LINE NUMBER...i know for inserting above or below a perticular line we can use sed a\ or i\.... thanks in advance.... (7 Replies)
Discussion started by: anj
7 Replies
Login or Register to Ask a Question