10

I want to add a text to the end of the first line of a file using a bash script. The file is /etc/cmdline.txt which does not allow line breaks and needs new commands seperated by a blank, so text i want to add realy needs to be in first line.

What i got so far is:

line=' bcm2708.w1_gpio_pin=20'
file=/boot/cmdline.txt
if ! grep -q -x -F -e "$line" <"$file"; then
  printf '%s' "$line\n" >>"$file"
fi

But that appends the text after the line break of the first line, so the result is wrong. I either need to trim the file contend, add my text and a line feed or somehow just add it to first line of file not touching the rest somehow, but my knowledge of bash scripts is not good enough to find a solution here, and all the examples i find online add beginning/end of every line in a file, not just the first line.

1
  • 1
    grep "$line" "$file" use this instead @RyuKajiya I have just tried it working fine on my terminal. Commented Nov 16, 2014 at 15:34

4 Answers 4

25

This sed command will add 123 to end of first line of your file.

sed ' 1 s/.*/&123/' yourfile.txt

also

sed '1 s/$/ 123/' yourfile.txt

For appending result to the same file you have to use -i switch :

sed -i ' 1 s/.*/&123/' yourfile.txt
Sign up to request clarification or add additional context in comments.

7 Comments

What about sed '1 s/$/ 123/' ? =)
Yeah that also will work thanks @sputnick I'm adding this too.
sed ' 1 s/.*/&bcm2708.w1_gpio_pin=20/' "$file" > "$file" just cleared the whole file...
Check again @RyuKajiya, updated the command as per your requirement.
Thanks, first writing to a temp file, then moving to the wanted file works! Now i just have a problem that the check if the file allready contains the text doesnt work. It allways adds the text, even if it is allready there
|
4

This is a solution to add "ok" at the first line on /etc/passwd, I think you can use this in your script with a little bit of 'tuning' :

$ awk 'NR==1{printf "%s %s\n", $0, "ok"}' /etc/passwd
root:x:0:0:root:/root:/bin/bash ok

5 Comments

So i would do printf awk 'NR==1{printf "%s %s\n", $0, "bcm2708.w1_gpio_pin=20"}' /boot/cmdline.txt >> "$file" ?
Do you really need sudo here ? I suspect no! For the rest, seems fine !
Doesn't work, i edited the printf line to the one in above comment, but the file does not get modified
awk don't modify the file in-place, you need to use a temporary file or use sed -i ...
This updates the first line, but gets rid of every other line.
2

To edit a file, you can use ed, the standard editor:

line=' bcm2708.w1_gpio_pin=20'
file=/boot/cmdline.txt
if ! grep -q -x -F -e "$line" <"$file"; then
    ed -s "$file" < <(printf '%s\n' 1 a "$line" . 1,2j w q)
fi

ed's commands:

  • 1: go to line 1
  • a: append (this will insert after the current line)
  • We're in insert mode and we're inserting the expansion of $line
  • .: stop insert mode
  • 1,2j join lines 1 and 2
  • w: write
  • q: quit

Comments

0

This can be used to append a variable to the first line of input:

awk -v suffix="$suffix" '{print NR==1 ? $0 suffix : $0}'

This will work even if the variable could potentially contain regex formatting characters.

Example:

suffix=' [first line]'
cat input.txt | awk -v suffix="$suffix" '{print NR==1 ? $0 suffix : $0}' > output.txt

input.txt:

Line 1
Line 2
Line 3

output.txt:

Line 1 [first line]
Line 2
Line 3

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.