1

The focus of this question shifted a bit, the real problem is addressed in Edit 2 below. The problem at the start:

I want to append a simple string, for example the letter N, to all lines in my file. This must happen without introducing linebreaks. What I need looks like this:

file     result
Aa       AaN
Bb       BbN
C        CN
Dd       DdN

The only thing I found so far is

sed -e 's/../&N/'

which appends the string only at a certain location, so for entries with varying character length this does not work as the result looks like this:

AaN
BbN
C
N
DdN

The most simple analogon is

sed -e 's/^/&N/' file

which prepends each line. So I need the reverse parameter of ^ that allows me to append to each line, no matter how long the string is in that line. The other questions I found about appending introduce several other constraints that don't apply here.


Edit: The suggested

sed 's/$/N/' file > file2

only changes the very last line in my file., whereas I need the change in every line. It seems to work in the linked question but here it does not. I don't know why.


Edit 2:

As RavinderSingh13 pointed out, my list that I obtained by some other commands contained ^M characters. So by using

cat -v file

the real content was revealed:

Aa^M
Bb^M
C^M
Dd^M

The presented solution was able to fix this issue!

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
And
  • 241
  • 2
  • 12

3 Answers3

2

If you are ok with awk, you could simply use print instead of substitution too.

awk '{gsub(/\r/,"");print $0"N"}'  Input_file

In case you want to do for a specific field vice then try:(taking example where 1st field should be append with N here):

awk '{gsub(/\r/,"");$1=$1"N"} 1'  Input_file
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • Your first piece of code outputs only the very last line and even in the wrong fashion: ``Nd``. The second example looks correct, but again touches only the last line. – And Apr 18 '19 at 09:52
  • 1
    @And, could you please check if you have control M characters in your Input_file by doing `cat -v Input_file` and let us know. – RavinderSingh13 Apr 18 '19 at 10:14
  • 1
    it seems I do. Every element is listed with ``^M`` in between. I guess that changes the picture. – And Apr 18 '19 at 11:59
  • 1
    @And, I knew it, I updated my answers kindly check them and let me know then? I think you don't need `\r` so I have added logic to remove them from lines. – RavinderSingh13 Apr 18 '19 at 12:00
  • It changed now, but the output is stil not quite correct. Instead I get ``AaBbCDdN``. However when I write a new list in a fresh file the result is correct. Sadly my input file is generated by a special command and too long to write by hand, you can see here for details on how I obtained it: https://stackoverflow.com/questions/55726452/print-differences-between-not-sorted-strings-from-files, Maybe this helps overcome the problem. – And Apr 18 '19 at 12:15
  • 1
    @And, `\r` wouldn't come by commands it comes when a file is copied from windows to unix system, so either you could use `dos2unix` utility(after generating file run it to remove junk characters) OR use like `tr -d '\r < Input_file > temp_file && mv temp_file Input_file` and let me know then? – RavinderSingh13 Apr 18 '19 at 12:19
  • 1
    Okay, now I was able to do it. I freshly generated the list again and used your first line of code on it. It still contained ``^M`` characters as ``cat -v`` shows, but a linebreak after each as well. Now I have the desired output. Thank you for helping me pick up my mess! Do you have a suggestion on how to edit my post to make it useful to others? – And Apr 18 '19 at 12:33
1

All you need is:

sed 's/\r*$/N/'

Look:

$ cat -v file
Aa^M
Bb^M
C^M
Dd^M

$ sed 's/\r*$/N/' file | cat -v
AaN
BbN
CN
DdN
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • 1
    Very nice, this is even simpler. Since RavinderSingh13 helped me unravel my problem I leave his answer as accepted, else I would accept this answer now. – And Apr 23 '19 at 07:18
0

without -i option to get file changed, tried on gnu sed:

sed -E 's/$/N/' file