The Input file content is like:


Desired Output:


I have the command below with sed

sed -i 's/.//16' $inputFile

But deletes my header like:


To avoid that I tried to do:

tail -n +2 | sed -i 's/.//16' $inputFile

But is not working. Thanks in advance


Using awk:

awk -F ';' -v OFS=';' 'NR > 1 { sub("..$", "", $2) }; 1' <"$inputFile"

This would modify the second ;-delimited field on every line of input except from the first, by removing the last two characters from it.

The trailing 1 causes every line to be outputted (it may be replaced by { print }).

You would redirect the output of this to a new file which you later could replace the original with.

  • @StéphaneChazelas As usual, thanks. I'll learn and remember, one day. – Kusalananda Aug 21 '18 at 10:40

If two characters are to be removed before the second delimiter (and assuming like in your sample that there are always at least two characters before each of the first two delimiters), use:

sed -i -- '1!s/..;/;/2' "$inputFile"

The 1! avoids changing the header (first) line.
The rest removes two characters just before the delimiter ;.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.