Home > Software design >  Remove all the lines that contain a specific amount of characters before delimiter
Remove all the lines that contain a specific amount of characters before delimiter

Time:12-15

Input

test:123456
smt:123456
test2:123456
mmm:123456
random:123456

Expected output

test:123456
test2:123456
random:1234567

CodePudding user response:

Soo many options:

$ awk -F':' 'length($1) != 3 {print}' <in.txt
test:123456
test2:123456
random:123456
$ sed  '/^...:/d' <in.txt
test:123456
test2:123456
random:123456
$ grep -v '^...:' <in.txt
test:123456
test2:123456
random:123456
$ python -c "import csv,sys; print('\n'.join([':'.join(a) for a in list(csv.reader(sys.stdin,delimiter=':')) if len(a[0]) != 3]))" <in.txt
test:123456
test2:123456
random:123456
$ perl -F: -nae 'length($F[0]) == 3 || print' <in.txt
test:123456
test2:123456
random:123456
$ ruby -F: -nae '$F[0].length == 3 || print' <in.txt
test:123456
test2:123456
random:123456
$

CodePudding user response:

There are many potential solutions to your problem. Without knowing what you've tried we can only guess which solutions might be appropriate. Because you've tagged bash, perhaps this will suit:

#!/bin/bash
while read -r line
do
    tmp="${line%%:*}"
    if [ "${#tmp}" -gt 3 ]; then
        echo "$line"
    fi
done < input.txt
test:123456
test2:123456
random:123456
  •  Tags:  
  • bash
  • Related