pankaj_ar
pankaj_ar

Reputation: 765

how to take substring in ksh

I have a file named "output.txt" having data in format:

400949703|2000025967912|20130614010652|20130614131543
355949737|2144050263|20120407100407|20120407101307
355499738|2144500262|20110911010901|20110911135601

I am executing an awk command as shown below:

awk -F"|" '{num1="`echo $3| cut -c1-8`"; print $num1}' output.txt

My expected output is :

20130614
20120407
20110911

But I am getting output as what is actually the input.

400949703|2000025967912|20130614010652|20130614131543
355949737|2144050263|20120407100407|20120407101307
355499738|2144500262|20110911010901|20110911135601

Not able to find out the reason. My task is to compare the 1st 8 characters in 3rd and 4th column. But stucked at this part only.

Experts, kindly help me to get the way, where I am missing.

Upvotes: 0

Views: 1644

Answers (2)

fedorqui
fedorqui

Reputation: 289555

What about using cut twice?

$ cut -d'|' -f4 file | cut -c-8
20130614
20120407
20110911
  • Firstly to get the 4th field based on | delimiter.
  • Secondly to get the first 8 characters (note that cut -c-8 is the same as your cut -c1-8)

Upvotes: 1

Juan Diego Godoy Robles
Juan Diego Godoy Robles

Reputation: 14955

You're mixing bash with awk, one tool is just enough:

awk -F\| 'a=substr($3, 1, 8){if(a==substr($4, 1, 8)){print a}}' output.txt

Get substrings of columns 3 and 4 , compare it and print if its ok.

Upvotes: 1

Related Questions