Gnu tool; sed awk echo etc to prepend or append string to a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Gnu tool; sed awk echo etc to prepend or append string to a file
# 1  
Old 02-23-2017
Gnu tool; sed awk echo etc to prepend or append string to a file

Looking to add text to a file, example

File example;
Code:
nodegroups:
  check-hosts: L@host.domain.com,host2.domain.com,host3.domain.com

I need to take a file with a one line list of hosts separated by commas

Code:
host.domain.com,host2.domain.com,host3.domain.com

and prepend the string " check-hosts: L@" to the line of hosts above (line 2). I need to have two spaces before "check-hosts: L@"
My approach has been to prepend the string " check-hosts: L@" to the file containing the line of hosts. but I am open to other ways as long as I can use the file that contains a list of hosts as the starting poing.

Line 1 nodegroups: needs to be above line 2 in my example.
# 2  
Old 02-23-2017
Try:
Code:
cat << EOF > new_file
nodegroups:
  check-hosts: L@$(cat input_file)
EOF

or

Code:
{ 
  printf "nodegroups:\n  check-hosts: L@"
  cat input_file
} > new_file

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 02-28-2017
I have been attempting to replace "checkhosts" num=$RANDOM
How can i do this? Currently when i place single quotes, double quotes or no quotes I get an error..
Code:
num=$RANDOM
echo $num


Code:
{    printf "nodegroups:\n  $num: L@"   cat input_file } > new_file


Last edited by RudiC; 02-28-2017 at 04:26 PM..
# 4  
Old 02-28-2017
Difficult to believe.
I can't find an error:
Code:
num=$RANDOM
echo $num
9131
{  printf "nodegroups:\n  $num: L@";   cat file; }
nodegroups:
  9131: L@host.domain.com,host2.domain.com,host3.domain.com

What error message do you get?
# 5  
Old 02-28-2017
Quote:
Originally Posted by bash_in_my_head
I have been attempting to replace "checkhosts" num=$RANDOM
How can i do this? Currently when i place single quotes, double quotes or no quotes I get an error..
Code:
num=$RANDOM
echo $num


Code:
{    printf "nodegroups:\n  $num: L@"   cat input_file } > new_file

There needs to be either a newline or a semi-colon before the closing curly
brace..
# 6  
Old 02-28-2017
Hi Scrutinizer...

AND before 'cat', { printf "nodegroups:\n $num: L@" ; cat input_file ; } > new_file ...
('printf' ought to be formatted correctly for purity.)
This User Gave Thanks to wisecracker For This Post:
# 7  
Old 02-28-2017
Exactly Wisecracker, also the correct/recommended way to use printf is to not use values in the format string (first field), but use format specifiers instead:

Code:
{ printf "nodegroups:\n %d: L@" "$num"; cat input_file ;} > new_file

Another thing to note is that the first curly brace must be followed by space (space, tab, newline) (something that was done right here, but good to know)..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace string of a file with a string of another file for matches using grep,sed,awk

I have a file comp.pkglist which mention package version and release . In 'version change' and 'release change' line there are two versions 'old' and 'new' Version Change: --> Release Change: --> cat comp.pkglist Package list: nss-util-devel-3.28.4-1.el6_9.x86_64 Version Change: 3.28.4 -->... (1 Reply)
Discussion started by: Paras Pandey
1 Replies

2. Shell Programming and Scripting

Replace string in XML file with awk/sed with string from another

Sorry for the long/weird title but I'm stuck on a problem I have. I have this XML file: </member> <member> <name>TransactionID</name> <value><string>123456789123456</string></value> </member> <member> <name>Number</name> ... (9 Replies)
Discussion started by: cozzin
9 Replies

3. Shell Programming and Scripting

sed - Find a String and append a text end of the Line

Hi, I have a File, which have multiple rows. Like below 123456 Test1 FNAME JRW#$% PB MO Approver XXXXXX. YYYY 123457 Test2 FNAME JRW#$% PB MO Super XXXXXX. YYYY 123458 Test3 FNAME JRW#$% PB MO Approver XXXXXX. YYYY I want to search a line which contains PB MO Approver and append... (2 Replies)
Discussion started by: java2006
2 Replies

4. Shell Programming and Scripting

Append a searched string with another string using sed

Hi, I need to replace and append a string in a text if grep is true. For eg: grep ABC test.txt | grep -v '\.$' | awk {'print $4'} | sed "s/ ? How do I replace all instances of "print $4" using sed with another sring? Eg of the string returned will be, lx123 web222 xyz Want to... (8 Replies)
Discussion started by: vchee
8 Replies

5. Shell Programming and Scripting

perform echo and awk inside a string

hi, just wanted to make a shortcut of this one a="a b c" b=`echo $a | awk '{print $2}'` echo "the middle is $b" why can't i do this: a="a b c" echo "the middle is ${`echo $a | awk '{print $2}'`}" <- bad substitution :wall: thanks (6 Replies)
Discussion started by: h0ujun
6 Replies

6. Shell Programming and Scripting

sed to find first appearance and append string

I have a file like below #GROUP A belongs to Asia GROUP A jojh hans local admin GROUP A gege fans michel jing jong #GROUP U belongs to USA GROUP U jeff goal hello world My requirement is to grep for first apperence of GROUP A which is not commented and append my name to end of file.... (12 Replies)
Discussion started by: vkk
12 Replies

7. Shell Programming and Scripting

sed append to string

I am trying to replace in multiple files every instance of text that begins with http and add hyperlink characters to it. I can get it to work with the following:sed -e "s/http*.*/<a href=\"&\">&<\/a>/g" * as long as the http text is at the end of the file. I need it to stop at the end of the... (2 Replies)
Discussion started by: numele
2 Replies

8. Shell Programming and Scripting

prepend and append characters to the same line

Hi, I have a file 'tmp.dat' that contains the below data: 81763 40829 30405 80452 I want to prepend a ' character to the beginning of every line, and I want to append ', at the end of every line. Below is what I expect: '81763', '40829', '30405', '80452', Can anyone help me? ... (3 Replies)
Discussion started by: ChicagoBlues
3 Replies

9. Shell Programming and Scripting

Append and Prepend Text to a file list

I want to print out a directory listing, then append ] to the end of each line. I'm trying to create a list of Wiki links based on folder listings that i can just copy and paste without having to edit 100's of file listings. Using sed i've figured out to do something like this: sed... (4 Replies)
Discussion started by: CapnDoody
4 Replies

10. Shell Programming and Scripting

echo, append to end of file

I need the line printed with echo to append to eof of to exactly line, am i able to do that? i mean echo "sysctl -w lalala=1" > to end of file /etc/sysctl.conf or to the 21st line, if the line exist, open new line and insert text there. Thx.maybe i'm in wrong topic but anyway... (2 Replies)
Discussion started by: hachik
2 Replies
Login or Register to Ask a Question