37

I would like to add character A at the end of each line in a text file. How can I do this with awk?

1AAB
VBNM
JHTF
2SDA

Desired output

1AABA
VBNMA
JHTFA
2SDAA
Thor
  • 45,082
  • 11
  • 119
  • 130
user1676953
  • 371
  • 1
  • 3
  • 3
  • 1
    You can do this pretty easily with sed - `sed -i 's/$/A/' file.txt` – squiguy Sep 17 '12 at 07:58
  • Be careful to ```-i``` flag, it overrides file.txt in-place – EnthusiastiC Mar 17 '21 at 22:03
  • You can use this ```sed -e '1 s/$/400/' -e '2 s/$/401/' -e '3 s/$/402/' -e '4 s/$/403/' file.txt``` to add at end of each line a specified character (e.g., here I used numbers 400-403). The number 1,2, and 3 reflects the line index. – EnthusiastiC Mar 17 '21 at 22:10

1 Answers1

60

this may do the job for you

awk '{print $0"A"}' yourFile
Kent
  • 189,393
  • 32
  • 233
  • 301
  • 3
    If you don't want to hard-code the character, you can pass it to awk: `awk -v char=A '{print $0 char}' file` – glenn jackman Sep 17 '12 at 10:19
  • not sure where to put this, but this `'{print $0"A"}'` resulted in an extra newline output after each `$0` for some reason. This baffled me. Somehow a `\n\r` got inserted after each `$0`. I solved this issue using `tr` afterwards. Something like this `./script.awk -F $'\t' file.tsv | tr "\n\r" ""` might work for you – Matthias Sep 21 '16 at 15:09
  • 9
    Getting some weird results here: `awk '{print $0"A"}' yourFile` ---- `1AABA VBNMA JHTFA 2SDAA` As expected. However: `awk '{print $0"A"}' foo.txt` Puts the "A" at the beginning of each line. `Aser_id,count_seen A,44 A,18603 A,87452 A,16572` – alexplanation Aug 04 '17 at 22:19
  • 4
    I think the weird behaviour is because of trailing "\r" character. You can solve it by doing `sed 's/\r//g' foo.txt | awk '{print $0",1"}'` – guillefix Nov 07 '19 at 17:41
  • @guillefix Great! This is exactly what I am looking for. – ssavva05 Jan 27 '21 at 17:41