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:
LINE]Foo
but when I do this:
/[A-Z]/a\
] Foo
it prints foo on a new line:
LINE
]Foo
how can I get sed to append text without the new line?
Try something like this:
I will add Eon at the end of the line
$ 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
1 Like
echo "LINE" | sed 's/$/]FOO/'
This appends the "]FOO" to end of the line.
Thanks,
Kalai
1 Like
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:
TITLE
this is a line
this is a line
then i what it to be
TITLE]Foo
this is a line
this is a line
Thank you
try this
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
1 Like
It can be written in sed one liner. Here is how:
$ 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
1 Like
sed 's/^[A-Z][A-Z]*$/&\]FOO/' infile
or with whitespace tolerance:
sed 's/^[ \t]*[A-Z][A-Z]*[ \t]*$/&\]FOO/' infile
1 Like
thank a lot guys your answers helped me but now i have one more problem
i want to append to the end of a line that starts with a specific character and this line can have any character and spaces in it
soft of like this:
this is a line
- this line starts with a minus
+ this line starts with a plus
this is another line
and i want to be able to append //foo to the end of that line:
this is a line
- this line starts with a minus //foo
+ this line starts with a plus //foo
this is another line
how can I do that i've been trying to adapt the solutions that you guys posted for mi first post but so far I haven't been able to make it work
Thank you for you help.
awk or sed?
awk '$1~"^[+-]"{$0=$0 " //foo"}1' infile
sed '/^[ \t]*[+-]/s|$| //foo|' infile
1 Like
thanks a lot you guys you solved all my problems
sed ' /^[[:upper:]]\+$/ s/$/]FOO/' filename
note:
\+ is a GNU sed extension and this will not work with other seds..