3

I would like get this string line

AUGUSTYN|Stanisław|3589238

without | and :

I tried something like that:

cat baza|grep "AUGUSTYN" -n|awk -F '|' '{print $1,$2,$3,$4}'`

(baza is my file with text, and AUGUSTYN is the name of person)

Result of this command is:

1:AUGUSTYN Stanisław 3589238 

As you see there still this pipe :( And I would like to get this:

1 AUGUSTYN Stanisław 3589238

I take text from this base file:

AUGUSTYN|Stanisław|3589238
BARAN|Stanisław|3511477
BUCZEK|Marek|3511526
CABAJ|Wanda|3511483
CEPAK|Józef|3511067

Getting strings without | and : would be great but I will have to get the number of line where this text is stored. So I had to use grep. If you can find any other more obvious solution for this too It would be mega great.

How can I archieve it? I started it learning an hour ago this awk. Please help.

1
  • 2
    Try -F '[|:]'. That removes both pipes and semi colons.
    – lgarzo
    Jan 27, 2013 at 16:20

2 Answers 2

2

grep is not necessary, you can do this with awk alone:

awk 'BEGIN{FS="|"} $1 == "AUGUSTYN" { print NR " " $1 " " $2 " " $3}'
1
  • 4
    +1. You can take advantage of awk's output field separator (default a space) like this: awk -F\| '{$1=$1; print NR, $0}' -- the $1=$1 part forces awk to rewrite the input line using the output field separator. This method allows you to avoid hardcoding the number of fields. Jan 27, 2013 at 18:00
0

How about adding | sed "s/:/ /" at the end of your command? so it's like this

cat baza|grep "AUGUSTYN" -n|awk -F '|' '{print $1,$2,$3,$4}' | sed "s/:/ /"

Unless I'm misunderstanding you and you want to do that in 1 command.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .