Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-07-2012
Registered User
 

Join Date: Feb 2012
Posts: 4
Thanks: 6
Thanked 0 Times in 0 Posts
sed append without using new line

im trying to append to the end of the line using sed but I want to do it without creating a new line
the text to which I want to append is all in capital letters.

I want to do something like this:


Code:
LINE]Foo

but when I do this:


Code:
/[A-Z]/a\
] Foo

it prints foo on a new line:

Code:
LINE
]Foo

how can I get sed to append text without the new line?
Sponsored Links
    #2  
Old 02-07-2012
Registered User
 

Join Date: Jun 2011
Posts: 65
Thanks: 6
Thanked 14 Times in 14 Posts
Try something like this:
I will add Eon at the end of the line

Code:
$ cat data
Knight Eon
Hello World
Hello Earth
How is everyone

$ sed -e '$s/\(.*\)/\1Eon/g' data
Knight Eon
Hello World
Hello Earth
How is everyone Eon

The Following User Says Thank You to knight_eon For This Useful Post:
mrjavoman (02-07-2012)
Sponsored Links
    #3  
Old 02-07-2012
Registered User
 

Join Date: Jan 2009
Location: Pune ,India
Posts: 130
Thanks: 12
Thanked 22 Times in 22 Posts

Code:
echo "LINE" | sed 's/$/]FOO/'

This appends the "]FOO" to end of the line.

Thanks,
Kalai

Last edited by Franklin52; 02-07-2012 at 02:56 AM.. Reason: Please use code tags for code and data samples, thank you
The Following User Says Thank You to kalpeer For This Useful Post:
mrjavoman (02-07-2012)
    #4  
Old 02-07-2012
Registered User
 

Join Date: Feb 2012
Posts: 4
Thanks: 6
Thanked 0 Times in 0 Posts
thanks for the replays but what I really want to do is append to only the lines that are in all capital letters.

sort of like if i Have:

Code:
TITLE
this is a line
this is a line

then i what it to be

Code:
TITLE]Foo
this is a line
this is a line

Thank you
Sponsored Links
    #5  
Old 02-07-2012
Registered User
 

Join Date: Jan 2009
Location: Pune ,India
Posts: 130
Thanks: 12
Thanked 22 Times in 22 Posts
try this


Code:
touch /tmp/test.$$
while read line
do
check=`echo $line | tr -dc [A-Z]`
if [ ! -z "$check" ];then
echo "$line" | sed 's/$/]Foo/' >> /tmp/test.$$
else
 echo "$line" >> /tmp/test.$$
fi
done < one
cat /tmp/test.$$

one:
TITLE
this is a line
this is a line

Output:

TITLE]Foo
this is a line
this is a line

Last edited by Franklin52; 02-07-2012 at 02:58 AM.. Reason: Code tags
The Following User Says Thank You to kalpeer For This Useful Post:
mrjavoman (02-07-2012)
Sponsored Links
    #6  
Old 02-07-2012
Registered User
 

Join Date: Jun 2011
Posts: 65
Thanks: 6
Thanked 14 Times in 14 Posts
It can be written in sed one liner. Here is how:


Code:
$ cat data
Knight Eon
Hello WORLD
Hello Earth
LINE
How is everyone
$ sed -e 's/\(\L.*\)/\1]Foo/g' data
Knight Eon
Hello WORLD]Foo
Hello Earth
LINE]Foo
How is everyone

Hope this will help you
The Following User Says Thank You to knight_eon For This Useful Post:
mrjavoman (02-07-2012)
Sponsored Links
    #7  
Old 02-07-2012
Scrutinizer's Avatar
mother ate her
 

Join Date: Nov 2008
Location: Amsterdam
Posts: 5,371
Thanks: 80
Thanked 1,107 Times in 1,011 Posts

Code:
sed 's/^[A-Z][A-Z]*$/&\]FOO/' infile

or with whitespace tolerance:

Code:
sed 's/^[ \t]*[A-Z][A-Z]*[ \t]*$/&\]FOO/' infile

The Following User Says Thank You to Scrutinizer For This Useful Post:
mrjavoman (02-07-2012)
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
find a certain line and append text to the end of the line peachclift Shell Programming and Scripting 2 12-19-2011 03:10 PM
Append next line to previous line when one pattern not found kannansr621 Shell Programming and Scripting 4 09-12-2011 03:31 PM
Append each line to next previous line in a file sudhakaryadav Shell Programming and Scripting 8 06-01-2009 11:21 AM
Append line that does not contain pipe to it previous line ainuddin Shell Programming and Scripting 11 11-11-2008 09:58 AM
Joining lines in reverse. append line 1 to line 2. dwalley Shell Programming and Scripting 7 08-04-2008 07:11 AM



All times are GMT -4. The time now is 04:32 AM.