1

this is a recurrent topic but still not duplicate since I tried all the solutions and none worked for me.

I am trying to add the suffix "_1" to a list of identifiers imported from a txt file. The goal is to add this into a loop in a function.

GSE72343.txt:

SRR2182285
SRR2182286
SRR2182287
SRR2182288
SRR2182289

and I want to retrieve this:

SRR2182285_1
SRR2182286_1
SRR2182287_1
SRR2182288_1
SRR2182289_1

I've tried a couple of suggestions in other threads here and here but I am getting wrong outputs like showed below:

for i in $(cat GSE72343.txt); do echo "$i" "$i_1"; done
RR2182285
RR2182286
RR2182287
RR2182288
RR2182289

sed 's/$/ _1/' GSE72343.txt _12182285 _12182286 _12182287 _12182288 _12182289

awk '{ print $0, "_1" }' GSE72343.txt _12182285 _12182286 _12182287 _12182288 _12182289

Any advices? Thanks a lot!

αғsнιη
  • 41,407
  • 1
    In your for bash for loop, you need to use {} around your variable. for i in $(cat GSE72343.txt); do echo "$i" "${i}_1"; done

    The way you have it currently, it is trying to reference the variable i_1, when really you want to reference the variable i and tack on _1 to the end of it. Using curly brackets in quotes allows you to do this.

    – Natolio Apr 07 '21 at 18:42

2 Answers2

5

Your issue here is that your file has Windows-style line endings (\r\n). That's why your commands fail. If you fix the file, they should work as expected. If you have dos2unix, you can do:

dos2unix GSE72343.txt

But if not, you can also do it with sed:

sed -i 's/\r$//' GSE72343.txt

After that your commands will work (with minor changes). And, as a proof of concept:

$ printf 'SRR2182285\r\nSRR2182286\r\n' > GSE72343.txt

$ cat GSE72343.txt SRR2182285 SRR2182286

$ for i in $(cat GSE72343.txt); do echo "$i" "${i}_1"; done RR2182285 RR2182286

$ sed -i 's/\r$//' GSE72343.txt

$ for i in $(cat GSE72343.txt); do echo "$i" "${i}_1"; done SRR2182285 SRR2182285_1 SRR2182286 SRR2182286_1

Finally, if you don't wan to modify your original file, you can use any of the approaches you have in your question but just stick a | tr -d '\r' at the end. For instance:

$ sed 's/$/_1/' GSE72343.txt | tr -d '\r'
SRR2182285_1
SRR2182286_1
SRR2182287_1
SRR2182288_1
SRR2182289_1
terdon
  • 242,166
0

Using your code awk '{ print $0, "_1" }' GSE72343.txt but without the comma:

awk '{ print $0 "_1" }' file
SRR2182285_1
SRR2182286_1
SRR2182287_1
SRR2182288_1
SRR2182289_1