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.   

Frown
  • 197
  • So 43 is length including the tab and ^M or excluding both? – Inian May 07 '19 at 07:32
  • @Inian, including both characters. – Frown May 07 '19 at 07: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 08 '19 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 '19 at 02:15

1 Answers1

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.

laenkeio
  • 581
  • 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 07 '19 at 08: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 07 '19 at 09: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 07 '19 at 10:10