0

I want a Bash script that puts userinput into a file called software.txt. But I need the userinput to be under a specific line #addedsoftware in software.txt. The script should be able to add useriput multiple times into this file. Is this something that can be achieved with something like sed? When i ran sed '/#TESTING/a some text here' software.txt it did not add it to the file. Then i ran sed '/#TESTING/a some text here' > software.txt This one just adds whatever keys i press after the command is ran.

Edit i got it working with.: sed -i '/#TESTING/a some text here' file

Cyrus
  • 84,225
  • 14
  • 89
  • 153
akkunt
  • 9
  • 3
  • Does [Insert line after match using `sed`](https://stackoverflow.com/questions/15559359/) or [How to insert text after a certain string in a file?](https://unix.stackexchange.com/questions/121161/) answer your question? – U880D Jan 24 '22 at 10:48
  • The first one did help but i am facing a problem.``` sed '/#TESTING/a some text here' software.txt``` does not actually put it in the file it just displays it as a one time thing. – akkunt Jan 24 '22 at 11:09
  • Right, depending on your environment you would either to do an inline edit via `sed -i '...` or writing the result into another file via `> software.txt.updated.` – U880D Jan 24 '22 at 11:14
  • 1
    The information on how you tried _sed_ should be in your question, not in a comment. You can edit your question (there is a tiny _edit_ link below your question). – user1934428 Jan 24 '22 at 11:16

1 Answers1

1

Would something like this work? It takes the software.txt file and splits it into two parts, top and bottom. Top is everything up to the specified line and bottom is everything after that line. Then it puts the software.txt file back together with the user input between the two parts.

For example say your software.txt file looks like this.

software.txt

# Software.txt

Line 3
Line 4
Line 4
Line 6

# Put user input below this line.

Line 10
Line 11
Line 12

insert_user_input.sh

#!/usr/bin/env bash

# Read user input.
echo -n "Enter input: "
read user_input

software_txt_top_half=$(sed '/# Put user input below this line./q' software.txt)

software_txt_bottom_half=$(sed '1,/# Put user input below/d' software.txt)

# Insert user input into software.txt after specified line.
cat << EOF > software.txt
$software_txt_top_half
$user_input
$software_txt_bottom_half
EOF

Then after running the script it would look like this,

$ ./insert_user_input.sh 
Enter input: asdfasdfasdfasdfasdfasdf
# Software.txt

Line 3
Line 4
Line 4
Line 6

# Put user input below this line.
asdfasdfasdfasdfasdfasdf

Line 10
Line 11
Line 12
  • that doesnt really make use of sed in any meaningful way.. and instead of echoing over and over you could just use cat. your most critical piece is using grep and awk more than sed... – UpAndAdam Jan 24 '22 at 23:18
  • As per your suggestions, I've replaced the multiple echo statements with a heredoc and replaced the grep and awk commands with sed expressions. Thank you for the feedback. – BlueSquare23 Jan 25 '22 at 17:13