LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-26-2006, 02:51 PM   #1
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Rep: Reputation: 32
Question Sed empty lines


Hello. First of all, I am not new to Unix but I am quite new to scripting. I am trying to place a word at the beginning of each line using sed. It doesn't work on lines with only one word. . Both of these commands to the same thing. They put global at the beginning of each line except empty and single-word lines. Please help.

sed '/[^ ] /s/^[^ ]/global &/'g bla2
sed '/[^^$] /s/^[^^$]/global &/'g bla2
 
Old 08-26-2006, 03:14 PM   #2
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
sed 's/^/global /'
This replaces the beginning of the line with "global " and leaves everything else on the line in tact. The stuff before the first s/ is not necessary in this case, because if you leave it off then sed matches all lines.
 
Old 08-26-2006, 03:24 PM   #3
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Original Poster
Rep: Reputation: 32
Thank you. Actually I needed something that puts global at the beginning of each non-empty line. But I managed to figure that out by myself. Thank you very much.
 
Old 08-27-2006, 05:17 AM   #4
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
It's always nice to include the solution to your problem in the thread in case someone else is searching for the same solution in the future.

Put global on empty lines
sed '/^$/s/^/global/g' /path/to/filename

Put global on non-empty lines
sed '/^$/!s/^/global/g' /path/to/filename

sed one-liners

Last edited by /bin/bash; 08-27-2006 at 05:23 AM.
 
Old 08-27-2006, 06:37 AM   #5
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Original Poster
Rep: Reputation: 32
Thank you. Both the examples and the link are great!!!

Edit:
And for those that do not want to match non-empty lines with blank characters:

sed '/[^ ]\{1,\}/ s/^/global /'

Last edited by nc3b; 08-27-2006 at 07:12 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Join all lines using sed chipix Programming 3 04-03-2007 09:55 AM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM
Help deleteing lines using Sed nazs Programming 5 03-07-2006 12:38 AM
Removing duplicate lines with sed tireseas Programming 10 01-12-2005 03:27 AM
Searching for 2 empty lines by RegEx in perl mosh Programming 8 09-08-2004 02:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration