LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to split the fields of file based on field length (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-split-the-fields-of-file-based-on-field-length-4175608837/)

Shruthi GM 06-29-2017 07:07 AM

How to split the fields of file based on field length
 
Hi,

Im trying to split the fields of file based on field length. The output should be written to same new file. Im trying like below but nothing is happening. Could some one suggest me what wrong I have done here or how can this be achieved?

awk -F, '{if length($4) > 5 print > "newfile" ; if length($5) > 5 print > "newfile" ; else print > "oldfile"}' original_file

Thanks in advance.

wpeckham 06-29-2017 07:24 AM

Google (or duckduckgo search) for string handling and one of the engine names. Normal string handling by index or location will serve you well here.
Engines that are readily available in a non-development machine are BASH, awk, sed, perl, and there are others.
I would do it directly in Bash unless you have other reasons that mandate awk.

It is easier than you think.

BW-userx 06-29-2017 08:13 AM

I do not use awk but I bet it is your formatting of the equation

http://www.unix.com/shell-programmin...using-awk.html

AwesomeMachine 06-29-2017 08:53 PM

I might mention python too.

syg00 06-30-2017 12:35 AM

Quote:

Originally Posted by Shruthi GM (Post 5728655)
Im trying like below but nothing is happening.

Something would certainly be happening - notification of syntax errors for a start.
"if" requires brackets around the test - all of them. Then you can work on the likely logic error.


All times are GMT -5. The time now is 08:56 PM.