LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 07-15-2010, 11:37 AM   #1
secondchanti
LQ Newbie
 
Registered: Jul 2010
Posts: 24

Rep: Reputation: 15
how to cut two columns in data file and print file


Friends,
Iam having following DATA file.

123456789rama1001
147852369sama1002
963258741kama1003

I want sorted out put file as below

123456789 1001
147852369 1002
963258741 1003

ie two coloumns with one space in between two columns is required.
which CUT COMMAND is to be used.
------------------------------------------------------------------------
similarly iam having the following PRINT file .
NAME ROLL DOB

RAMYA 10002 2010
KRISH 20002 2011
SATYA 30002 2013
SUBHA 40002 2014
SAMYA 50002 2014

I WANT THE FOLLOWING SORTED PRINT FILE.

RAMYA 2010
KRISH 2011
SATYA 2013
SUBHA 2014
SANYA 2014

ie two columns with one space in between, which command is to be used ?
========================================================================

similarly i am having another print file of 400 pages with heading like below :
ABC COMPANY LIMITED
BRANCH OFFICE :XYZ
----------------------------------------------------
SL NO NAME OF PH DOB AGE STATUS LOAN REMARKS
-----------------------------------------------------
DATA .....
DATA .....
.........
...........

Now freinds, pl tell me with what COMMAND IN SINGLE STROKE I CAN DELETE THE HEADING IN ALL THE 400 PAGES (ie all heading like company ltd, branchoffice, slno, name,dob,age etc . i want the raw file as below

DATA.........
DATA.........
...........
..........

PLEASE SUGGEST THE COMMANDS.
[mod_edit]we don't do e-Mail support; utterly against the spirit of a community board[/mod_edit]
Thanks and regards
RAO.

Last edited by Tinkster; 07-16-2010 at 01:52 PM. Reason: [mod_edit]
 
Old 07-15-2010, 11:56 AM   #2
rdgreenlaw
Member
 
Registered: May 2007
Location: Newport, Maine, USA
Distribution: Debian 8.7
Posts: 73

Rep: Reputation: 18
Not sure about the others, but AWK can take the file that is split into columns by spaces and remove the middle column

Code:
awk -F " " '{print $1 $3}' < ExistingFileNameGoesHere > NewOutputFileNameGoesHere
Sort can be used to change the sequence (based on the first column)

put
sort < NewOutputFileNameGoesHere > SortedNewFileNameGoesHere

To sort on the second column reverse $1 and $3 above, run the sort and then do

awk -F " " '{print $2 $1}' < SortedNewFileNameGoesHere > FinalResultFileGoesHere
 
Old 07-15-2010, 12:25 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
I will be very happy if you mail the details to my email id.
That is not how a forum operates---the answers should be posted for everyone to see.

Further, by exposing your e-mail address, you are inviting a spam attack.
 
Old 07-15-2010, 12:30 PM   #4
ljm
LQ Newbie
 
Registered: Jan 2010
Posts: 5

Rep: Reputation: 0
RAO,
In your first example.
Import your data into a spreadsheet application. I will assume you import your data into Open Office Calc, as it is a free application.
Once imported I will assume for each of your rows of data that the data row fills a single cell. For this explanation I will assume your data is located in cell A1 to A3. Cell A1 would have the following: 123456789rama1001
In cell B1 enter the command =LEFT(A1,13) in cell C1 enter the command =RIGHT(A1,4)
=LEFT(A1,13) this formula says to go to cell A1 and select the 13 leftmost characters
=RIGHT(A1,4) this formula says to go to cell A1 and select the 4 rightmost characters
You will get 2 columns of data that look like 123456789rama 1001. However if you click on the cells B1 + C1 you will see the formulas that you entered not the data. So the next thing you need to do is convert these formulas to values.
Highlight and Copy cell B1 and C1 to cell D1 and E1. The trick here is after you highlight and select B1 and C1 highlight cell D1 and right click to select Paste special. When the dialog box opens, in the selection area, ensure that value is selected and formula isn't selected. Click OK.
Then use the above instructions to copy this to your remaining rows of data.
If you print the data and need a space between the 2 columns of data insert a column to separate the 2 data columns.
Once that's done you'll have 2 discrete columns of data for sorting as you please.
NOTE. In your examples all the rows had the same # of characters. If each row of data is of different lengths then this will not work.

Example 2
Do the above import
in cell B1 use the same formula as above but change 13 to 5, as you only want the first 5 leftmost characters.
in cell C1 use the formula =MID(A6,7,5) This formula says go to cell A1, go over 7 characters or spaces and select the next 5 characters.
In cell D1 use the same =right formula above.
As above copy data in B1, 1 and D1 to cells E1, F1 and G1 by doing a paste special as above.
Then use the above instructions to copy this to your remaining rows of data.
Once that's done you'll have 3 discrete columns of data for sorting as you please. In your example you can either hide column F and print columns E and G or copy columns E and G to another location and print that range.
If you print the data and need a space between the 2 columns of data insert a column to separate the 2 data columns.
NOTE. In your examples all the rows had the same # of characters. If each row of data is of different lengths then this will not work.

Example 3
Do the above import
Do a global data sort.
Once the sort is done all the headings will be in one location delete those rows and resort the data as you like.
If you have to keep the data in the same order and don't want to resort the data after you sort and delete the headers.
Enter a column in the leftmost section of the spreadsheet in cell A1 insert 1, in cell A2 enter 2. Highlight cell A1 and A2, drag them to the end of you data and Calc will enter sequential numbers.
Sort this column with your data. After deleting the header rows, just sort on column A and your data will be in the original order without the headers.
 
Old 07-15-2010, 12:42 PM   #5
dezmond
LQ Newbie
 
Registered: Dec 2006
Posts: 8

Rep: Reputation: 1
First Example Solution
----------------------
For the first example above, the following works on my system from the command line:
Code:
sed -e 's/[a-z]\{1,\}/ /' filename.txt >newfilename.txt
Second Example Solution
-----------------------
For the second example above, the following works on my system from the command line:
(Note that the "+3" after "tail" is the count of lines from the beginning of the file where you want to start -- to get rid of the header.)

Code:
tail +3 filename.txt | awk '{ printf("%s %s\n",$3,$1) }' | sort | awk '{ printf("%s %s\n",$2,$1) }' > outputfile.txt
Third Example solution
----------------------
For the third example, if every data line begins with the word "DATA", the solution is simply:

Code:
grep ^DATA filename.txt >outputfile.txt
If, however, the data is not prefixed by "DATA", the following sed command will remove the header lines:

Code:
sed -e '/^ABC.*/d' -e '/^BRANCH.*/d' -e '/^SL.*/d' -e '/^---/d' inputfile.txt > outputfile.txt
(Obviously, change "ABC", "BRANCH", "SL", and "---" to match the actual values of your data file.)

Last edited by dezmond; 07-15-2010 at 01:06 PM. Reason: added second and third example solutions
 
Old 07-15-2010, 01:19 PM   #6
rahulkya
Member
 
Registered: Feb 2009
Location: New Delhi
Distribution: Linux mint,Ubuntu,Debian,RHEL 5,slackware 13.1, free BSD,solaris.
Posts: 186
Blog Entries: 3

Rep: Reputation: 33
use command cut to separate two outputs eg:
cut -c 1-9 filename > file1
cut -c 14-17 filename >file2
then use paste command as:
paste -d " " file1 file2

BUT this is not better than reply from dezmond(4th reply)..
{
NAME ROLL DOB

RAMYA 10002 2010
KRISH 20002 2011
SATYA 30002 2013
SUBHA 40002 2014
SAMYA 50002 2014

I WANT THE FOLLOWING SORTED PRINT FILE.

RAMYA 2010
KRISH 2011
SATYA 2013
SUBHA 2014
SANYA 2014
}

For this use this:
cut -d" " -f1,3 filename


trying the solution of ur last question...
 
Old 07-16-2010, 06:01 AM   #7
bsat
Member
 
Registered: Feb 2009
Posts: 347

Rep: Reputation: 72
@Rao it would be really great if you can try something yourself and then post the question. That is the only way you will learn
 
  


Reply



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
Get data from multi lined text file using awk, sed or perl - grep & cut not upto par cam34 Programming 4 07-02-2010 03:10 AM
Text file manipulation: selecting specific lines/columns using awk and print CHARL0TTE Linux - Newbie 2 02-27-2010 02:40 AM
How to print data in rows and columns suran Linux - General 3 03-15-2009 02:53 PM
How to use command grep,cut,awk to cut a data from a file? hocheetiong Linux - Newbie 7 09-11-2008 07:16 PM
Reading in data in columns from a file (C++) Nylex Programming 4 03-08-2006 05:55 PM

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

All times are GMT -5. The time now is 06:12 PM.

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