1
printf "%s\t" HEMES n{1..1600} >file_a
sed -i 's/\t*$//' file_a > file_a

removes a tab which is caused at the end of the line after the first line of the code (where the stuff is printed) .

My problem is now that i would need to remove the tab behind that line (starting with HEMES) in a larger file which has more lines. If I just try the sed line that does not work. The line I want to remove the character from is always the first line of the file .

2
  • 2
    sed -i 's/\t*$//' file_a > file_a will wipe out your file. Did you mean sed -i 's/\t*$//' file_a?
    – muru
    Jul 19, 2015 at 5:14
  • What do you mean with "behind" that line? Also it's not clear what you want to do, please post an example of your current file and an example of how you'd like it to be.
    – kos
    Jul 19, 2015 at 6:21

1 Answer 1

1

Instead of doing everything in one line and outputting to file, separate it in two parts - output the HEMES string and then append the n{1...1600} sequence: printf "%s" HEMES > file_a; printf "%s\t" n{1..1600} >> file_a.

If you specifically need to remove that tab between HEMES and n1 in an already existing file, here's an awk solution:

awk ' {sub(/[[:blank:]]/,""); print} ' file_a > tmp_file && cat tmp_file > file_a.

What this does is take sub function, which deletes the first occurrence of the tab, we output that text to a temporary file, and then take those contents back to file_a. The reason for the switching between files is this: unless you are using awk version that allows inline editing (gnu awk, I believe), this will work in any case - awk, mawk, nawk, etc.

1
  • The awk master is back :)
    – A.B.
    Jul 21, 2015 at 4:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .