Search code examples
unixawksubstringkshcut

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.


Solution

  • 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)