How to print blank lines before and after???


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to print blank lines before and after???
# 1  
Old 04-03-2010
How to print blank lines before and after???

Dear all,

I'm a new guy in Linux scripting. Basically, I have no idea on how to write a script.

Now, I have a problem in preparing my gaussian inputs.
For example, I have a file like this

File name: input.gjf
Code:
H 1.418400 1.180500 -1.247900
H -1.894500 1.628800 -0.000200
Product
H 1.852200 1.607500 -0.107200
H -1.439600 1.956100 -0.069400

I want get a new modified file, like this
newinput.gjf
Code:
H 1.418400 1.180500 -1.247900
H -1.894500 1.628800 -0.000200

Product

1 5
H 1.852200 1.607500 -0.107200
H -1.439600 1.956100 -0.069400

Simply to say, I want to 1) find the line "Product"; 2) add an blank line before line "Product"; 3) add an blank line and line with content "1 5"after it.
I'm wondering by using "awk" to find the line and print these four new lines(two blank lines and a line with "1 5"). Is that possible?

Thank you in advance!

Please help me~~ Grazie!
# 2  
Old 04-03-2010
Code:
awk '$0 ~ "Product" { $0="\nProduct\n\n1 5" } {print}' t
H 1.418400 1.180500 -1.247900
H -1.894500 1.628800 -0.000200

Product

1 5
H 1.852200 1.607500 -0.107200
H -1.439600 1.956100 -0.069400

t is the input file with the contents you have shown.
# 3  
Old 04-03-2010
Awk syntax:
/^Product/ { print ""
print $0
print ""
print "1 5"
next
}
{ print } # default for other lines
# 4  
Old 04-03-2010
OK, both work! thanks a lot.
# 5  
Old 04-03-2010
Shorter Smilie
Code:
awk '/Product/{$0=RS$0RS}1' file

# 6  
Old 04-03-2010
Quote:
Originally Posted by danmero
Shorter Smilie
Code:
awk '/Product/{$0=RS$0RS}1' file

Shorter, but incomplete. Smilie
# 7  
Old 04-03-2010
Code:
sed 's/^Product/\n&\n\n1 5/' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To check Blank Lines, Blank Records and Junk Characters in a File

Hi All Need Help I have a file with the below format (ABC.TXT) : ®¿¿ABCDHEJJSJJ|XCBJSKK01|M|7348974982790 HDFLJDKJSKJ|KJALKSD02|M|7378439274898 KJHSAJKHHJJ|LJDSAJKK03|F|9898982039999 (cont......) I need to write a script where it will check for : blank lines (between rows,before... (6 Replies)
Discussion started by: chatwithsaurav
6 Replies

2. UNIX for Advanced & Expert Users

Delete blank spaces and blank lines in a file

Hi Gurus, Somebody can say me how to delete blank spaces and blank lines in a file unix, please. Thank you for advanced. (10 Replies)
Discussion started by: systemoper
10 Replies

3. Shell Programming and Scripting

String search and print next all lines in one line until blank line

Dear all I want to search special string in file and then print next all line in one line until blank lines come. Help me plz for same. My input file and desire op file is as under. i/p file: A1/EXT "BSCABD1_21233G1" 757 130823 1157 RADIO X-CEIVER ADMINISTRATION BTS EXTERNAL FAULT ... (7 Replies)
Discussion started by: jaydeep_sadaria
7 Replies

4. UNIX for Dummies Questions & Answers

Finding lines with a regular expression, replacing them with blank lines

So the tag for this forum says all newbies welcome... All I want to do is go through my file and find lines which contain a given string of characters then replace these with a blank line. I really tried to find a simple command to do this but failed. Here's what I did come up with though: ... (2 Replies)
Discussion started by: Golpette
2 Replies

5. Shell Programming and Scripting

Print lines between a regExp & a blank line

Hi, I have a file, say files_list, as below (o/p of ls -R cmd) $ cat files_list /remote/dir/path/to/file: sub-dir1 sub-dir2 sub-dir3 ... /remote/dir/path/to/file/sub-dir1: remote_file1.csv.tgz <blank line 1> /remote/dir/path/to/file/sub-dir2: remote_file2.csv.tgz <blank... (3 Replies)
Discussion started by: dips_ag
3 Replies

6. Shell Programming and Scripting

Delete blank lines, if blank lines are more than one using shell

Hi, Consider a file named "testfile" The contents of file are as below first line added for test second line added for test third line added for test fourth line added for test fifth line added for test (5 Replies)
Discussion started by: anil8103
5 Replies

7. Shell Programming and Scripting

Print lines after the search string until blank line is found

All I want is to look for the pattern in the file...If I found it at # places... I want print lines after those pattern(line) until I find a blank line. Log EXAMPLE : MT:Exception caught The following Numbers were affected: 1234 2345 2346 Error java.lang.InternalError:... (3 Replies)
Discussion started by: prash184u
3 Replies

8. Shell Programming and Scripting

print lines AFTER lines cointaining a regexp (or print every first and fourth line)

Hi all, This should be very easy but I can't figure it out... I have a file that looks like this: @SRR057408.1 FW8Y5CK02R652T length=34 AGCAGTGGTATCAACGCAGAGTAAGCAGTGGTAT +SRR057408.1 FW8Y5CK02R652T length=34 FIIHFF6666?=:88@@@BBD:::?@ABBAAA>8 @SRR057408.2 FW8Y5CK02TBMHV length=52... (1 Reply)
Discussion started by: kmkocot
1 Replies

9. Shell Programming and Scripting

Unix help to find blank lines in a file and print numbers on that line

Hi, I would like to know how to solve one of my problems using expert unix commands. I have a file with occasional blank lines; for example; dertu frthu fghtu frtty frtgy frgtui frgtu ghrye frhutp frjuf I need to edit the file so that the file looks like this; (10 Replies)
Discussion started by: Lucky Ali
10 Replies

10. UNIX for Dummies Questions & Answers

How to count lines - ignoring blank lines and commented lines

What is the command to count lines in a files, but ignore blank lines and commented lines? I have a file with 4 sections in it, and I want each section to be counted, not including the blank lines and comments... and then totalled at the end. Here is an example of what I would like my... (6 Replies)
Discussion started by: kthatch
6 Replies
Login or Register to Ask a Question