Linux Cut Command
How do I Select Columns From a File?
The406378:Sales:Itorre:Jan
031762:Marketing:Nasium:Jim
636496:Research:Ancholie:Mel
396082:Sales:Jucacion:Ed
If you want to print just columns 1 to 6 of each line (the employee serial numbers), use the -c1-6 flag, as in this command:
cut -c1-6 company.data
406378
031762
636496
396082
If you want to print just columns 4 and 8 of each line (the first letter of the department and the fourth digit of the serial number), use the -c4,8 flag, as in this command:
cut -c4,8 company.data
3S
7M
4R
0S
And since this file obviously has fields delimited by colons, we can pick out just the last names by specifying the -d: and -f3 flags, like this:
cut -d: -f3 company.data
Itorre
Nasium
Ancholie
Jucacion
It's often the case that you want to use a space as the delimiter. To do so, you must put the delimiter in single quotes, like this: -d' '
Also, when you want to cut from a starting point to the end of the line, just leave off the final field number, as shown in the example below.
Let's say this is your test.txt file:
abc def ghi jkl
mno pqr stu vwx
yz1 234 567 890
To cut only columns 2-END, do this: cut -d' ' -f2- test.txt
And the results are:
def ghi jkl
pqr stu vwx
234 567 890
Here is a summary of the most common flags for the cut command:
-c [n | n,m | n-m] Specify a single column, multiple columns (separated by a comma), or range of columns (separated by a dash).
-f [n | n,m | n-m] Specify a single field, multiple fields (separated by a comma), or range of fields (separated by a
dash).
-dc Specify the field delimiter.
-s Suppress (don't print) lines not containing the delimiter.
Previous Lesson: Eliminating Duplicates
Next Lesson: Selecting Records
Comments - most recent first
(Please feel free to answer questions posted by others!)
cat file | awk 'BEGIN{FS=";"} {for(i=1;i<NF;i++) print $i}' | sort
Really need some help (and sorry for my English, i'm come frome France),
I'm on ubuntu, bash.
I've got a file with a lot of names delimited with a ";" written like this:
Francois de france; Julien d'amerique; Emilie de Suede; gustave de Picardie;
It's very difficult to read it, cause i have hundreds of them.
I'm trying to sort them like this:
Francois de France;
Julien d'Amerique;
(...)
Gustave de Picardie;
So i tried to use the ";" as a delimiter and reading the arguments:
cut -d ";" -f 1-100 file.txt
It only give me my first style list in the terminal....I'm completly blocked...
Is there here a Linux Champion that can help me..?
Thanks all !!!
Is there any difference between using -f flag and -c flag for a file given below for instance? Could you also provide an example where it matters which of those 2 flags you use?
AAAA BBBB CCCC
AAAA BBBB CCCC
try this..
sed -n -e '1'p -e '$'p shree.txt
Display all of the lines in the file where the last field is 4 digits long.
Search for "," before the field to make sure your regular expression matches
the whole field, use character classes to match four digits, and anchor it
to the end of the line to make sure it's the last field.
got that command..its very usefull
cut -d' ' -f3 <file1 >file2
I have a file like shree.txt. how to fetch the first record and last record by using unix command?
Thanks, Advanced
rev infile | cut -d'/' -f1 | rev
You can also do it with awk:
awk -F'/' '{print $NF}'
/oss/orbit/server/abc.txt
/oss/orbit/server/scripts/abc1.txt
The output should be
abc.txt
abc1.txt
s.no name date
1 a 21
2 b 22
3 c 23
i want to retrieve and display all values under name column or sometime name and date columns both.
name
a
b
c
Please tell me the command to display following name in short manner
John Milan Robort
J M R (short form)
244 1961287 559 302 2091182 273 224 172 353 254 440 68 224 299 325 309 284 283 265 129 185 958 221 769 557 465 350 2751361 629
245 2551501 616 346 2091395 299 231 202 393 299 463 70 256 326 375 322 304 307 295 132 1911009 245 956 656 545 370 3561585 700
there are 5 spaces at the beginning of each group of numbers. how do I average the third number in each group?
"ls, ps" together..mean i want to run this commands at the sametime bieng seprated by a commaa?
echo "9999 1.7260E+01 3.5783E-03" | awk '{FS = " "} {print $3}'
ls -l | sed '/^d/d'
this command does?
I ran it in Secure Shell, it looks like it eliminates all the Directories and subdirectories. Am I correct?
Please suggest.
And @Chem,, I guess you could do something like
cut -d' ' -f3
since the numbers seem to be separated by spaces (assuming that you treat "9999", "1.7260E+01" and "3.5783E-03" as different numbers.
Can I cut the third number? If the text is like this:
...
9999 1.7260E+01 3.5783E-03 ...
...
Assuming I don't know how many columns are there for each number.
Thanks for your time!
Chem
Don't apologize for your English. Most Amreicans barely speak correct English, let alone a foreign language.
n is the variable which contain more than 2 line numbers which i want to delete ....but its not working for more than 2 line numbers...........please give me solution
i want to elect only 3rd column and save in different file how can i do..
ATOM 2 CA ASN A 1 0.395 3.556 1.478
ATOM 2 CA ASN A 1 0.395 3.556 1.478
ATOM 2 CA ASN A 1 0.395 3.556 1.478
I'm Joseph. I speak englis a little (I'm Hungarian man). Sorry.
Please help.
The operation system of my desktop machine is Ubuntu linux 10.04, the language is Hu, encoding is utf8 (default).
My example data file is "cities.data, this file have got 1 line, 1 world:
$ cat cities.data
Székesfehérvár
This is OK, it is 14 characters.
The cut program:
$ cut -c 1-14 cities.data
Székesfehérv
It is 12 chars!
$ cut -c 1-2 cities.data
Sz
OK.
$ cut -c 1-3 cities.data
Sz
This is bad.
$ cut -c 3 cities.data
(empty)
$ cut -c 4 cities.data
ďż˝
What is the problem?
Please Help!
Joseph
controller: lun-pathname device filename adapter protocol lun size lun state
fas3170-201-77: /vol/sanboot_192_rhel6/sanboot_192 /dev/sdd host6 iSCSI 40.0g (42953867264) GOOD
fas3170-201-77: /vol/sanboot_192_rhel6/sanboot_192 /dev/sdc host5 iSCSI 40.0g (42953867264) GOOD
I want to extract only the words which are /dev/sda or /dev/sdc like that. how can i do this. Please help.
thank you
sort a comma-separated list
by domain, GET request, username, and/or response code using shell scripting
www.google.com,GET /blah,jeromy,404
www.yahoo.com,GET /index.html,nate,200
www.bing.com,GET /lame_search_engine,lameuser,200
www.aj.com,GET /?,nate,404
get the list sorted by user name with all the fields still in tact.
so have the output look something like:
jeromy www.google.com GET /blah
nate www.yahoo.com GET /index.html
lameuser www.bing.com GET/lame_search_engine
nate www.aj.com GET /?
echo `grep userone /etc/group | cut -d\: -f 1`
Assume that the user is in several groups and all we want is the output to be something like
nt, it, hr, cos.
I tried piping it through tr but all I got was a comma and no space after each word.
echo`grep userone /etc/group | cut -d\: -f 1 | tr [:sapce:] [,]`
i want to print column 22 23 and 52 53 only if column 23 is A and 53 is B
this is from text file how do i do so
need a clarfication on the above post.
How to use a Hex code delimiter.
My hex code delimiter is '001' (Ctrl A)
Please help me with the code.
Many Thanks
Regards
Gatha
1 a
2 b
c d
and i want it to be displayed like
1 a
b
c
using sed command.
thnx.
Thanks a lot
doc1.doc
i want to change it to DOC1.doc (change only the name to uppercase)
plz help
head -2 | tail -1
can i able to cut a single row from an n rows output
for example
#cat /proc/cpuinfo | grep core
gives the output
ore id : 0
cpu cores : 2
core id : 2
cpu cores : 2
core id : 0
cpu cores : 2
core id : 2
cpu cores : 2
i need to cut the 2nd row only...what command shd i use
sed '$r Report.htm' <Header.htm> s_timestamp() + Monthly_Report.htm
Which I know will not work right. What command do I use to get the left 9 bytes from the timestamp? how do I append it to the name of the concatenated report?
Anyhelp would be great, thanks!
|b|c
wen two pipeline is encountered it should eliminate & return the result as
e|b|c
example
7322 seconds is equivalent to 2 hours 2 minutes 2 seconds.
cut -d':' -f1 /etc/passwd | awk '{print $1"@example.com"}'
like adm@example.com?
12131313 ??? asdadsa cmd
132213 ?? fafa ls
i want show result:
12131313 cmd
132213 ls
the 1st column is account and the 2 last columns need to be displayed but only for the first account
10000000|Jan|02-Feb-2006|5753|5855
10000001|Jan|03-Feb-2006|6397|6567
10000000|Feb|04-Mar-2006|5855|6060
10000001|Feb|27-Feb-2006|6567|6597
10000000|Mar|02-Apr-2006|6060|6320
10000001|Mar|01-Apr-2006|6597|6639
10000000|Apr|01-May-2006|6320|6384
10000001|Apr|02-May-2006|6639|6646
output
Month Consumption
----- -----------
Jan xxxxxx
Feb xxxxxx
Mar xxxxxx
Apr xxxxxx
May xxxxxx
Jun xxxxxx
--------------------------------
Total xxxxxx
the "XXX" has 2 be automatically filled by the code
example
23|bak|234|54
34|col|453|65
i want the result to be without the delimiter in between
ie:
234 54
453 65
Thank you so much for your help.
See you...
@Cape: OK I see what you mean now... I'm not sure how to do this with cut ( I guess I would also like to know this :) ), but if you have awk on your system you could try:
ls -l | awk -F" " '{print $<field>}'
OR simply
ls -l | awk '{print $<field>}'
Replace <field> with whichever column/field you would like to print out. All white spaces are treated equally.
e.g.
ls -l | awk -F" " '{print $6}'
OR
ls -l | awk '{print $6}'
Hope this helps!
But what I am trying to do is :
ls -l | cut -f6
to get only de size of all files in the directory (for example) but the command ls -l steel show me all collumns.
Thanks again.
@Jan - you're welcome!
@Cape - the default delimiter is <TAB> so you could, for example, run the same command without the '-d' option i.e.:
cut -f1-3 <filename>
this will give you the first 3 columns, where the delimiter is <TAB>.
Thank you very much.
You could used:
cut -d' ' -f1-3 <filename>
this will give you the first 3 columns, where the delimiter is <space> as specified by -d' '
For eg in above mentioned example :
file is
abc def ghi jkl
mno pqr stu vwx
yz1 234 567 890
if we wana result like
abc def ghi
mno pqr stu
yz1 234 567
Then what command need to be use..
I hope you understand me, thanks a lot.
ABC def.ghi.jkl
MNO pqr.stu.vwx
and i wanted ghi. Help is much appreciated!
cut -d'.' -f2
def.ghi.jkl
pqr.stu.vwx
234.567.890
and i only want ghi?
but i tried to use it in user defined script ,,,but is it didn't work ..how i can use in user defined script to give me the same result....?
i really need it in a my own script..
can you help me...
abc def ghi jkl
mno pqr stu vwx
yz1 234 567 890
To cut only columns 2-END, do this:
cut -d' ' -f2- tt
And the results are:
def ghi jkl
pqr stu vwx
234 567 890
I welcome your comments. However... I am puzzled by many people who say "Please send me the Linux tutorial." This website *is* your Linux Tutorial! Read everything here, learn all you can, ask questions if you like. But
don't ask me to send what you already have. :-) NO SPAM! If you post garbage, it will be deleted, and you will be banned.
Copyright © by -
All rights reserved - Redistribution is allowed only with permission.

