Insert a string instead of blank lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert a string instead of blank lines
# 8  
Old 05-08-2012
And always say what didn't work - show the error message you get or the wrong output of the command, if it worked.
Just to say "It doesn't work!" is not really helpful.
This User Gave Thanks to zaxxon For This Post:
# 9  
Old 05-08-2012
Try

Code:
sed 's/^$/####/g' <filename>|tee <filename>

This User Gave Thanks to elixir_sinari For This Post:
# 10  
Old 05-08-2012
Quote:
Originally Posted by oreka18
how can i insert a string sush as "###" instead of blank lines in a file?
What exactly is a "blank line" for you?

Is it a line with no characters on it? Then this is your solution:

Code:
sed 's/^$/###/' /path/to/sourcefile > /path/to/targetfile

Or is it a line with only (optional?) whitespace on it? Then your solution is this (replace "<spc>" and "<tab>" with literal spaces/tabs):

Code:
sed 's/^[<spc><tab>]*$/###/' /path/to/sourcefile > /path/to/targetfile


Btw.: someone has suggested to use "tee" to change the source file in place without using an intermediate file. This is a risky technique and i would like to discourage it. It could well happen that the source is written over before it is read completely and the behaviour in case of power loss is unpredicable.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 11  
Old 05-09-2012
i do your script in my file but it doesn't work!
in_file:
Code:
456  
 
654654 
 
6545

your script:
Code:
sed 's/^$/###'

and the output was:
Code:
456  
 
654654 
 
6545

without change!!! i'm using ubuntu 10.4

Moderator's Comments:
Mod Comment Please use code tags, thanks!

Last edited by zaxxon; 05-09-2012 at 07:25 AM.. Reason: code tags, see PM
# 12  
Old 05-09-2012
did you read the below two lines ?

What exactly is a "blank line" for you?

Is it a line with no characters on it? Then this is your solution:

execute the below command and see, whether you are seeing any blank line. ( if yes, that sed command will not work for you )

[dot] match any character ( including space)

Code:
 
grep . input.txt

# 13  
Old 05-09-2012
itkamaraj is spot on: i can't see if your file contains non-printable characters (such as whitespace) or not and therefore provided you with two possible solutions.

Trying only one of them without even reading (much less answering) my question is - sorry to be so blunt - negligently wasting others efforts to help you. The reader here is to put as much effort in trying to understand the given answers as the writers of same are putting into writing them up.

Furthermore:

Quote:
Originally Posted by oreka18
i do your script in my file but it doesn't work!

[...] your script:
Code:
sed 's/^$/###'

This is definitely not "my script", because it contains a syntax error which was not there in the script i gave you and it woldn't have produced the output you mentioned, because it wouldn't have produced no output at all save for the message stating the syntax error.

If you want to succeed in writing scripts you have to be precise in what you do and what you think. Right now you gave us a sloppily defined problem, applied the solutions given to you in a sloppy way, analysed the reasons only sloppily (or not at all) and reported back in a sloppy way.

Regardless of us being able to solve your problem despite your efforts to not let us i can guarantee you that your scripts will fail badly: not because of this or that syntactical or procedural error, but because you don't show the right attitude to writing scripts (or doing sysadmin work, for that matter) correctly.

I hope this helps.

bakunin
# 14  
Old 05-10-2012
Regarding blank lines. IMO awk is best suited for this since NF catches everything, empty lines plus lines that consist of any kind of whitespace.
Code:
awk NF file

will remove any empty line..

Last edited by Scrutinizer; 05-10-2012 at 06:52 PM..
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. Shell Programming and Scripting

Insert String every n lines, resetting line counter at desired string

I need to read a text file and insert a string every n lines, but also have the line counter restart when I come across a header string. Line repeating working every 3 lines using code: sed '0~3 s/$/\nINSERT/g' < INPUT/PATH/FILE_NAME.txt > OUTPUT/PATH/FILE_NAME.txt I cannot seem to find... (1 Reply)
Discussion started by: Skonectthedots
1 Replies

3. Shell Programming and Scripting

Find regex, place on individual lines and insert blank line before

Hello, I have a file that I want to be able to insert a new line before every instance of a regex. I can get it to do this for each line that contains the regex, but not for each instance. Contents of infile: Test this 1... Test this 2... Test this 3... Test this 4... Test this... (2 Replies)
Discussion started by: deneuve01
2 Replies

4. Shell Programming and Scripting

Inserting blank lines after string change

My input data looks like this ... -150 120 8 -150 122 7 -150 124 11 -150 126 8 -150 128 19 -150 130 13 -150 132 26 -150 134 38 -150 136 45 -150 138 62 -150 140 75 -150 142 110 -150 144 139 -150 146 138 -150 148 158 -150 150 173 -150 152 217 (5 Replies)
Discussion started by: chrisjorg
5 Replies

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

6. Shell Programming and Scripting

insert blank line between lines

Hello, I am trying to write a script that will count the number of characters for each line in a file and all the lines that have less than 80 characters and that are ending with a period, I want it to insert a blank line after them immediately. But, for whatever reason the condition if ]] is... (3 Replies)
Discussion started by: Pouchie1
3 Replies

7. Shell Programming and Scripting

Insert string in alternate lines

Hi All, In continuation of my previous thread 'Add text at the end of line conditionally', I need to further modfiy the file after adding text at the end of the line. Now, I need to add a fixed charater string at alternate lines starting from first line using awk or sed.My file is now as below:... (10 Replies)
Discussion started by: angshuman
10 Replies

8. UNIX for Dummies Questions & Answers

Insert Text on lines having the string word

I need help on how I can accomplish my task. I hope someone can help me since I've researching and trying to accomplish this for hours now. Basically, I need to comment-out (or insert a # sign in the beginning of the line) a line when the line has the specific word I am searching. Example I have... (3 Replies)
Discussion started by: Orbix
3 Replies

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

10. Shell Programming and Scripting

Find a string and place two blank lines

Hi friends, I am looking for a line to find a particular string in my file and once found then replace with 2-3 blank lines before the string Example: aaa 11 bbb 1 2 3 aaa 22 bbb 4 5 6 Output (4 Replies)
Discussion started by: shaliniyadav
4 Replies
Login or Register to Ask a Question