How To Change Comma Delimited To Pipe Delimited In Unix
The cutting command in UNIX is a overnice utility program that allows you to cut data from a text file. The Linux cut control allows you to cut information by character, past field, or by column. if used correctly along with sed, discover, or grep in UNIX, the cutting can do lots of reporting stuff. For example, you can extract columns from a comma-separated file or a pipe or colon-delimited file using cutting command. For example, if you are merely interested in the start 2 columns y'all can prove them using this control. In this Linux cut command tutorial we will run across different options of cutting command, dissimilar examples of Linux cut command, and some important points about cut in UNIX.
In order to demonstrate the ability of cut command through various examples, we will following the colon-delimited text file equally input. This text file contains details of popular smartphones in 2011. The file contains 5 columns i.e. model, company, cost, camera, and 4G in the same order.
Here is the content of the file for your reference
Original File
trader@asia:~/perl cat list-of-smartphones-2014.txt
Model:Visitor:Price:Photographic camera:4G
IPhone4:Apple:chiliad$:Yes:Aye
Milky way:Samsung:900$:Aye:Aye
Optimus:LG:800$:Yes:Yes
Sensation:HTC:400$:Yep:Yeah
IPhone4S:Apple tree:1100:Yes:Yes
N9:Nokia:400:Yeah:Yeah
How to use the Cutting control in Linux
Now, let'southward meet some of the frequently used examples of cut command in Linux. You will larn about how to extract columns from a text file in Linux, extracting columns past characters, using thecut command with any delimiter e.one thousand. colon or pipe, extracting columns by the tab character, how to display the starting time column, and how to display multiple columns from the text file using thecut control in Linux.
i.How to Cut Text by Columns in UNIX
Cut by Cavalcade is easy, you first need to decide a delimiter, default is tab then you need to specify column number with -f option,f stands for a field, for example, cut -f1 file will display start cavalcade data. Since our file is colon-delimited, nosotros can utilize a colon as a delimiter and and then extract columns as shown in the following instance:
$ cut -d: -f1 list-of-smartphones-2011.txt Model IPhone4 Galaxy Optimus Sensation IPhone4S N9
You tin run into that only information from the first column is displayed.
2. Unix Cut by Characters
In the following example of the cutting command in UNIX will cutting by characters from 1st to 9th character. you can check each line of the output is exactly 9 characters long.
$ cutting -c 1-9 list-of-smartphones-2011.txt Model:Com IPhone4:A Milky way:Sa Optimus:Fifty Sensation IPhone4S: N9:Nokia:
3. Unix Cut by a delimiter
The tab character is the default delimiter for cut command. and "-f" option is used to cutting by a delimiter. You tin can override delimiter by providing the "-d" option. Following UNIX or Linux cut command case will show yous how to split a line by delimiter in UNIX.
$ cut -d: -f2 list-of-smartphones-2011.txt Company Apple Samsung LG HTC Apple tree Nokia
In this example delimiter is a colon i.e.":" grapheme, specified merely after "-d" and we have displayed values from the second column. If you want to display values from the 3rd column then simply give iii after -f e.1000. -f3, similarly for nth column just give -fn. See these best Linux books if yous want to acquire more nearly this option.
4. UNIX cut +sed command example with a tab delimiter
To show you an example of the cut control with tab delimiter, we need to first modify our delimiter from ":" to tab , for that nosotros tin use the sed command, which will replace all colon with \t or tab character. Subsequently that, we can use, and so we will utilise the cut command of Linux to extract the first column.
Hither is the verbal command you can use to do that
$ sed 's/:/\t/g' list-of-smartphones-2011.txt | cut -f 1 Model IPhone4 Galaxy Optimus Sensation IPhone4S N9
5. How to Cut the First Grapheme of a Line in UNIX
Following is an case of the cut in UNIX that will display the first character of each line from the input file.
$ cut -c 1 listing-of-smartphones-2011.txt Grand I One thousand O S I N
You can run into that merely the first character from each line is displayed.
6. How to display the kickoff column from a delimited file
Sometimes, we need the value from the offset cavalcade from a comma-separated or colon-separated file. that'due south very easy with UNIX/Linux cut command. define a delimiter and specify a column number. here is an example of UNIX to cutting the first column.
$ cut -d: -f1 listing-of-smartphones-2011.txt Model IPhone4 Galaxy Optimus Sensation IPhone4S N9
seven. Displaying multiple columns on output using the cut command in Linux
You lot can show more than than one cavalcade using Linux cut command. The post-obit case will show both first and second columns from a file.
$ cutting -d: -f '1 2' list-of-smartphones-2011.txt Model:Company IPhone4:Apple Galaxy:Samsung Optimus:LG Sensation:HTC IPhone4S:Apple tree N9:Nokia
Y'all can run across that both column 1 and column 2 are displayed together.
Important points on cut command in UNIX and Linux
Permit's revisit some important things about cut command in *NIX operating system. It's 1 of your helpful mates when awk command is not available.
1) The cut command is used to display selected parts of file content in UNIX.
2) The default delimiter in cut command is "tab", yous can change the delimiter with the option "-d" in the cut command.
3) The cut command in Linux allows you lot to select the part of the content by bytes, by character, and by field or column.
4) The cutting control in UNIX or Linux tin work with files or y'all can pipage it with the output of other UNIX/Linux commands.
five) In UNIX, cut -d command is used to cutting past a delimiter.
half-dozen) The cut -c command option is used to get line segments by characters.
Hither is a summary of important cut command examples in Linux:
That's all about cut control in UNIX and Linux. I dear this control for its simplicity in power, clever use of the cut control is capable of parsing log files, extracting the details you really need in ane column, and they you tin use the power of Excel to filter and sort that equally per your demand. In UNIX you can combine the output of the cut command to uniq and sort to filter duplicates and print them in sorted order.
Other Linux command tutorials you may like
- How to utilise the netstat command to find which process is listening on a port? (example)
- How does the nslookup command work in UNIX? (answer)
- x examples of lsof command in Linux? (examples)
- How to send HTTP requests to phone call a web service from the control line? (case)
- 10 books to acquire essential commands of Linux or UNIX? (listing)
- How Linux works? What Every SuperUser should know? (book)
- x Examples of cURL command in Linux? (examples)
- How to convert an IP accost to the hostname in Linux? (command)
- How to exit from the telnet command in UNIX? (instance)
- A Applied Guide to Linux Commands, Editors, and Shell Programming (guide)
Thanks for reading this article, if yous like this commodity then delight share it with your friends and colleagues. If yous have any suggestions or feedback then please drop a comment. If you want to learn more about powerful Linux command utilities and then read this book.
Source: https://javarevisited.blogspot.com/2017/04/10-examples-of-cut-command-in-unix-and-Linux.html
Posted by: hancockrouressedly.blogspot.com
0 Response to "How To Change Comma Delimited To Pipe Delimited In Unix"
Post a Comment