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.

file is:

BASH.NIR
SH.ABI

I want the awk will show:

User is NIR, SHELL is BASH
User is ABI, SHELL is SH

I dont know how to split a parameter by char. The Idea is:

cat file.txt | awk '{print "User is " afterDot($1) ", SHELL is " beforeDot($1)}'
share|improve this question

2 Answers 2

up vote 3 down vote accepted

You can use the string functions in awk.

$ (echo BASH.NIR; echo SH.ABI FOOBAR) | awk '{p=index($1,".");print "User is", substr($1,p+1) ", SHELL IS", substr($1,0,p-1)}'
User is NIR, SHELL IS BASH
User is ABI, SHELL IS SH

The index function returns the position of the character to be found (in this case a dot). And strstr will return a substring. We use p+1 and p-1 to not include the dot.

For more information look in the "String Functions" section of the awk manpage.

share|improve this answer

You can just use . as the field separator:

awk -F. '{ print "User is " $2 ", SHELL is " $1 }' file.txt
share|improve this answer
    
the text file can also have whitespaces –  nir Dec 22 '13 at 15:17
4  
@nir Please paste a sample of your exact input into your question, I can't guess what it looks like unless you represent it properly. –  Chris Down Dec 22 '13 at 15:18
1  
@nir - it's always best to include real example data. Contrived data usually just wastes the answer's time since it generally leads to the wrong solutions. –  slm Dec 22 '13 at 15:22
    
you right, will do form next time. –  nir Dec 23 '13 at 7:13

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.