I'm tring to remove the last 4 characters from strings in a file i.e.
cat /tmp/test
iwishicouldremovethis
icouldremovethos
so i would end up with the last 4 characters from each of the above i.e.
this
thos
I thought of using cut -c ... but I'm not sure how many characters will precede the important "Last 4 characters i need".
Thanks for your help!
Scott
2
You want to remove the last four characters or keep them?
You say remove them, then say:
Remove
sed "s/....$//" file1
iwishicouldremove
icouldremove
Keep
sed "s/.*\(....$\)/\1/" file1
this
thos
bash
while read -r line
do
echo ${line:(-4)}
done < "file"
danmero
4
or AWK
awk '{print substr($0,(length($0)-3))}' FILE
while read line; do
echo ${line%????}
done < infile
---------- Post updated at 22:39 ---------- Previous update was at 22:31 ----------
awk '{sub(/....$/,"")}1' infile
---------- Post updated at 22:43 ---------- Previous update was at 22:39 ----------
rev infile23|cut -c5-|rev
Scott
6
Enough already! You need to get out more
ps: nice!
Thanks :), but actually all my examples are wrong because of the OP's first sentence. Oh well... I probably should get out more
rev infile23|cut -c-4|rev
Scott
8
Not so fast, Mr S. The question isn't clear!