LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-29-2017, 07:07 AM   #1
Shruthi GM
LQ Newbie
 
Registered: Jun 2017
Posts: 2

Rep: Reputation: Disabled
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.
 
Old 06-29-2017, 07:24 AM   #2
wpeckham
Senior Member
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 3,850

Rep: Reputation: 1739Reputation: 1739Reputation: 1739Reputation: 1739Reputation: 1739Reputation: 1739Reputation: 1739Reputation: 1739Reputation: 1739Reputation: 1739Reputation: 1739
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.

Last edited by wpeckham; 06-29-2017 at 07:25 AM.
 
Old 06-29-2017, 08:13 AM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
I do not use awk but I bet it is your formatting of the equation

http://www.unix.com/shell-programmin...using-awk.html
 
Old 06-29-2017, 08:53 PM   #4
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,513

Rep: Reputation: 1010Reputation: 1010Reputation: 1010Reputation: 1010Reputation: 1010Reputation: 1010Reputation: 1010Reputation: 1010
I might mention python too.
 
Old 06-30-2017, 12:35 AM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 19,787

Rep: Reputation: 3574Reputation: 3574Reputation: 3574Reputation: 3574Reputation: 3574Reputation: 3574Reputation: 3574Reputation: 3574Reputation: 3574Reputation: 3574Reputation: 3574
Quote:
Originally Posted by Shruthi GM View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
converting File layout from pipe to fixed length based on control file kumar98 Linux - Newbie 1 12-23-2015 03:16 PM
Print fields to a file based on protocol type from netstat. vipinsqa Linux - Newbie 1 11-14-2014 08:40 AM
[SOLVED] Sorting filenames based on length of the file zama Linux - Software 5 03-26-2012 04:22 AM
Bash or PHP: Split csv file based on field value? guest Programming 4 02-06-2009 12:57 AM
how not to print the 4th field from a text file with six fields livetoday Red Hat 3 10-02-2007 01:19 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration