How to grep similiar values one by one under each other?

Viewed 86

I Have grep from file output like:

2531 POKRZYWNIAK KRZYSZTOF 244 18/01 2 13:46 23:26
3346 SOROTA DARIUSZ 244 18/01 1 04:05 13:46

Need to get it look like this:

2531 POKRZYWNIAK KRZYSZTOF 244 18/01 2 13:46 23:26
3346 SOROTA DARIUSZ_________244 18/01 1 04:05 13:46

without "" char - mean to space it until similiar/same values are one under one by each other. 4digit number under 4 digit number, name under name, 3digit name (ex.244) multichar like 18/01 string under 18/01, hour under hour, minutes under minutes. Output should look like below excluding "" char that i've used to demonstrate spaces I need.

I'm using cat filename.txt | grep -w $criteria

I'm also using grep with -i for case insensitive.

3 Answers

If the output is tab-delimited, use column like so:

cat filename.txt | grep PATTERN | column -t -s $'\t'

You can also use grep without the cat:

grep PATTERN filename.txt | column -t -s $'\t'

Could you please try following, based on your shown samples only written and tested in GNU awk. As per OP's description using regex for matching 3 digits space 2 digits/2 digits pattern in lines.

awk '
BEGIN{
  OFS="\t"
}
match($0,/[0-9]{3} [0-9]{2}\/[0-9]{2}.*/){
  firstPart=substr($0,1,RSTART-1)
  sub(/[^ ]* +/,"",firstPart)
  restPart=substr($0,RSTART,RLENGTH)
  sub(/ +/,OFS,restPart)
  print $1,firstPart,restPart
}
' Input_file | column -t -s $'\t'

Output will be as follows.

2531  POKRZYWNIAK KRZYSZTOF   244  18/01 2 13:46 23:26
3346  SOROTA DARIUSZ          244  18/01 1 04:05 13:46

Explanation: Adding detailed explanation for above solution.

awk '                                        ##Starting awk program from here.
BEGIN{                                       ##Starting BEGIN section of this program from here.
  OFS="\t"                                   ##Setting output field separator as TAB here.  
}
match($0,/[0-9]{3} [0-9]{2}\/[0-9]{2}.*/){   ##Using match function to match 3 digits space 2 digits/2 digits.
  firstPart=substr($0,1,RSTART-1)            ##Creating firstPart which has sub string from 1st position to till RSTART-1
  sub(/[^ ]* +/,"",firstPart)                ##Substituting till space everything with NULL in firstPart here.
  restPart=substr($0,RSTART,RLENGTH)         ##Creating restPart with substring of matched regex in match function.
  gsub(/ +/,OFS,restPart)                    ##Globally Substituting spaces with TAB in restPart.
  print $1,firstPart,restPart                ##Printing first field, firstPart and restPart here.
}
' Input_file | column -t -s $'\t'            ##Mentioning Input_file and sending awk output to column command to get good output.
$ awk -v OFS='\t' '{
    name = $0
    gsub(/[[:space:]]+/," ",name)
    gsub(/^[^ ]+ |( [^ ]+){5}$/,"",name)
    print $1, name, $(NF-4), $(NF-3), $(NF-2), $(NF-1), $NF
}' file | column -s$'\t' -t
2531  POKRZYWNIAK KRZYSZTOF  244  18/01  2  13:46  23:26
3346  SOROTA DARIUSZ         244  18/01  1  04:05  13:46
Related