How to make search, enter text and replace a line in bash?

Multi tool use
Multi tool use


How to make search, enter text and replace a line in bash?



I've been trying to write a bash code (experimenting) where bash will ask (Enter Text) and the user will enter a certain text lets say (I am new) and the text (I am new) will be moved to a line called User=I am new.



So, here is a cfg file named xyz.cfg


User
Name
Address
Country



and here is the bash script called test and with only echo $Input > $file the bash replace the whole file with the entered text but I want to keep the config as it is and add the Enter Text: value to the line User but this doesn't help, the code below,


#! /bin/sh
file=/root/xyz.cfg
echo "Enter text:"
read Input
echo $Input > $file **(confused here)** **(it replaces the whole cfg)**



Another line I thought of is here, but it doesn't work. I am not good with these so I hope it's solved; I tried searching the web with zero luck.



(doesn't work)


#! /bin/sh
file=/root/xyz.cfg
echo "Enter text:"
read Input
echo $Input > sed '/User/a' xyz.cfg



(doesn't work)


#! /bin/sh
file=/root/xyz.cfg
echo "Enter text:"
read Input
echo $Input > sed '/User/a' $file



Edit:



When a someone is asked for Enter Text: while running the bash script ./test and the someone typed Entered Text from bash promot the entered $Input (Entered Text from bash promot) should move to xyz.cfg and add the line to the Variable User:



Finally, the xyz.cfg should look like


User: Entered Text from bash prompt
Name
Address
Country



Before it looked like this,


User:
Name
Address
Country



Edit2: This GIF might explain what I need.





Are you wanting to be prompted for each replacement in the file, or just once for the entire file?
– Erayd
Jul 2 at 4:40





Once or maybe two three times, but for now lets just say once, But it should ask, Enter Line: and when user types in Enter Line: name Script should add name to line User in xyz.cfg
– Nov2
Jul 2 at 4:49






Could you please clarify what you actually need regarding the number of prompts? The difference between "once per file" and "once per User in the file" is significant.
– Erayd
Jul 2 at 5:02





Also, what should the file look like in the end?
– Benjamin W.
Jul 2 at 5:03





@Erayd #! /bin/sh file=/root/xyz.cfg echo "Enter text:" read Input echo $Input > $file (confused here) (it replaces the whole cfg)
– Nov2
Jul 2 at 5:06




1 Answer
1



This should do the trick:


#!/bin/bash
FILE=/tmp/test.cfg

echo -n "Enter text: "
read INPUT
sed -i "s/User:/& $INPUT/" "$FILE"



This uses sed to do an in-place search / replace (-i) which appends the entered text after the first occurence of User: on each line within $FILE.


-i


User:


$FILE



& refers to the matched portion of the text - it's essentially saying "replace <thing> with <thing> <entered text>."


&


<thing>


<thing> <entered text>



If you need to prompt for text each time a matching User: line is encountered, you can use the following approach:


User:


#!/bin/bash
FILE=/tmp/test.cfg
ORIGINAL=$(cat "$FILE")

truncate -s 0 "$FILE"
while read LINE; do
if `grep -q 'User:' <<< "$LINE"`; then
echo -n "Enter text: "
read INPUT < /dev/tty
sed "s/User:/& $INPUT/" <<< "$LINE" >> "$FILE"
else
echo "$LINE" >> "$FILE"
fi
done <<< "$ORIGINAL"



This second approach does the following:


User:





Thankyou very much it worked!
– Nov2
Jul 2 at 5:35





Second one worked too!
– Nov2
Jul 2 at 7:58






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

OA CFRjB buce A0nP,Iget Q,nZpTacmZXO2Q Y r7HQnVmu rIFZuDU 98yy8
TNWVI 9bPWWFsB KYdookDJuTIy0O2SqZgJKY,B u

Popular posts from this blog

Rothschild family

Cinema of Italy