0

Currently I am working on a folder of files, and each file have some lines as follows:

abcde    fghij
abcde    fghij
jklmn    pqrst
.....    .....

These lines have a specific line length 43. At the middle of each line is a tab character, and at the end is the windows line break character ^M. I would like to do the following steps:

 First, select these lines with line length 43
 Second, replace the tab in the middle with a comma
 Third, replace the line break character at the end with a dot.

And the expected output should be like this:

abcde, fghij.
abcde, fghij.
jklmn, pqrst.

I have tried sth as follows, but I failed:

 sed -i -e 's/^.\{43\}\r/ ./g' input.file

Does anyone know how to deal with this?

Update You can click this link to get a test file.   

4
  • So 43 is length including the tab and ^M or excluding both?
    – Inian
    May 7, 2019 at 7:32
  • @Inian, including both characters.
    – Frown
    May 7, 2019 at 7:34
  • I neither find a tab character in your test file nor a line of 43 characters. Better paste your file in the question (indend each line with four spaces for fformatting), so we know we are talking about the same thing.
    – Philippos
    May 8, 2019 at 15:17
  • @Philippos Thank you for your help. Sorry, I used awk print to count the number of characters, but it seems 43 is not correct,. The solution of laenkeio is correct, just change the number and everything is fine.
    – Frown
    May 14, 2019 at 2:15

1 Answer 1

0

Try this:

sed -ne '/^.\{43\}$/s/\t/, /g;/^.\{44\}$/s/\r/./gp' input.file > new.file

or if you're on mac os:

sed -ne $'/^.\{43\}$/s/\t/, /g;/^.\{44\}$/s/\r/./gp' input.file > new.file

I added a space after the comma to match the expected output. That's why the second match is for 44 rather than 43 characters.

3
  • Thank you for your help, but unfortunately it doesn't work for my situation. I upload a test file and you can make a test. Maybe there is sth incorrect with my problem description.
    – Frown
    May 7, 2019 at 8:25
  • I suspect this is a UTF-8 problem, given your test sample. It does not appear that the spacing in the sample is a regular tab.
    – laenkeio
    May 7, 2019 at 9:42
  • I tried to replace the tab with the same space in the command, but it didn't work. Have you tried the same way on the test sample?
    – Frown
    May 7, 2019 at 10:10

You must log in to answer this question.

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