Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
1  
Try -F '[|:]'. That removes both pipes and semi colons. –  lgarzo Jan 27 '13 at 16:20

2 Answers 2

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

awk 'BEGIN{FS="|"} $1 == "AUGUSTYN" { print NR " " $1 " " $2 " " $3}'
share|improve this answer
3  
+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. –  glenn jackman Jan 27 '13 at 18:00

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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