Get first column linux

How to get the first column of every line from a CSV file?

How do get the first column of every line in an input CSV file and output to a new file? I am thinking using awk but not sure how.

6 Answers 6

It will split each input line in the file data.txt into different fields based on , character (as specified with the -F ) and print the first field (column) to stdout.

@downvoter .. A downvote without explanation doesn’t help anyone (OP, SO or me). This is a functional solution that meets OP’s stated requirements. I am happy to correct errors or improve my answer but that requires constructive feedback.

I didn’t downvote, but I also won’t upvote: It’s the use of awk where cut would do. It smacks of one-size-fits-all-ism; using perl or sed would be just as bad. Not wrong, just not really right. Now, if you had answered with an awk script that handled a csv file like «last, first»,field2,field3 correctly, that would have been more appropriate.

@Sorpigal ..and I wouldn’t have downvoted you if you had used cut in place of awk 🙂 .. either tool is fine for this. FWIW, OP mentioned awk in their post, and I upvoted a «competing» cut solution (it could have been yours had you posted). It’s not a religion, it’s a small task that needed to be done, and I picked one of several tools to do it.

@Levon May be the down-voter saw your solution as an incomplete one. OP wanted the output to a new file. 😛

@JaypalSingh Ha ha .. yes, perhaps, but that would be somewhat petty (anyone using a linux system most likely would know how to use io redirection) and could have easily been noted by the downvoter (and then trivially fixed). OP didn’t seem troubled by that (nor do all of the answers provide this). Doesn’t matter, it solved OP’s problem which is main reason for the Q&A.

Источник

How to Print the First Column or Last Column or Both Using `awk`

Linux’s `awk` command is a powerful utility for different operations on text files such as search, replace, and print. It is easy to use with tabular data because it automatically divides each line into fields or columns based on the field separator. When you work with a text file that contains tabular data and want to print the data of a particular column, then the `awk` command is the best option. In this tutorial, we will show you how to print the first column and/or last column of a line or text file.

Читайте также:  Kali linux iso boot

Print the first column and/or last column of a command output

Many Linux commands such as the ‘ls’ command generate tabular outputs. Here, we will show you how to print the first column and/or last column from the output of the ‘ls -l’ command.

Example 1: Print the first column of a command output

The following `awk` command will print the first column from the output of the ‘ls -l’ command.

The following output will be produced after running the above commands.

Example 2: Print the last column of a command output

The following `awk` command will print the last column from the output of the ‘ls -l’ command.

The following output will be produced after running the above commands.

Example 3: Print the first and last columns of a command output

The following `awk` command will print the first and last columns from the output of the ‘ls -l’ command.

The following output will be produced after running the above commands.

Here, we will show you how to use the `awk` command to print the first column and/or last column of a text file.

Create a text file

To follow along with this tutorial, create a text file named customers.txt with the following content. The file contains three types of customer data: name with id, email, and phone number. The tab character (\t) is used to separate these values.

Jonathon Bing — 1001 jon@gmail.com 01967456323

Micheal Jackson — 2006 mic@gmail.com 01756235643

Janifer Lopez — 3029 jani@gmail.com 01822347865

John Abraham — 4235 john@gmail.com 01590078452

Mir Sabbir — 2756 mir@gmail.com 01189523978

Example 4: Print the first column of a file without using a field separator

If no field separator is used in the `awk` command, then a space is used as the default field separator. The following `awk` command will print the first column by using the default separator.

The following output will be produced after running the above commands. Note that the output shows only the customer’s first name because the space is applied as the field separator. The solution to this problem is shown in the next example.

Example 5: Print the first column of a file with a delimiter

Here, \t is used as a field separator to print the first column of the file. The ‘-F’ option is used to set the field separator.

The following output will be produced after running the above commands. The content of the file is divided into three columns based on \t. Therefore, the customer’s name and id are printed as the first column. If you want to print the customer’s name without the id, then continue to the next example.

If you want to print the customer’s name without the id, then you have to use ‘-‘ as a field separator. The following `awk` command will print the customer’s name only as the first column.

Читайте также:  Dd linux команда progress

The following output will be produced after running the above commands. The output includes the full names of the customers without their ids.

Example 6: Print the last column of a file

The following `awk` command will print the last column of customers.txt. Because no field separator is used in the command, the space will be used as a field separator.

The following output will be produced after running the above commands. The last column contains phone numbers, as shown in the output.

Example 7: Print the first and last columns of a file

The following `awk` command will print the first and last columns of customers.txt. Here, tab (\t) is used as the field separator to divide the content into columns. Here, tab (\t) is used as a separator for the output.

The following output will appear after running the above commands. The content is divided into three columns by \t; the first column contains the customer’s name and id and the second column contains the phone number. The first and last columns are printed by using \t as a separator.

Conclusion

The `awk` command can be applied in different ways to get the first column and/or last column from any command output or from tabular data. It is important to note that a field separator is required in the command, and if one is not provided, then the space is used.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.

Источник

How to capture first column values of a command?

I am new to shell scripting. I am trying to write a script that is suppose to run a command and use for loop to capture first column of the output and do further processing. command: tst get files output of this command is something like

NAME COUNT ADMIN FileA.txt 30 adminA FileB.txt 21 local FileC.txt 9 local FileD.txt 90 adminA 
#!/bin/bash for f in $(tst get files) do echo "FILE :[$]" tst setprimary $ && tst get dataload done 
FILE :[NAME] FILE :[COUNT] FILE :[ADMIN] FILE :[FileA.txt] FILE :[30] FILE :[adminA] FILE :[FileB.txt] FILE :[21] FILE :[local] FILE :[FileC.txt] FILE :[9] FILE :[local] FILE :[FileD.txt] FILE :[90] FILE :[adminA] 
FILE :[FileA.txt] FILE :[FileB.txt] FILE :[FileC.txt] FILE :[FileD.txt] 

What should I modify in the shell script to only capture NAME column values? Am I executing the tst get files command correctly in the for loop or is there a better way to execute a command and loop thru the results?

3 Answers 3

EDIT (Samuel Kirschner): you can do without the for loop entirely and just use awk to print the lines you’re interested in

If you want to keep the for loop for some reason and just extract the file name from the lines while skipping the header, you have a few choices. Awk is probably the easiest because of the NR builtin variable (which counts lines) and automatic field-splitting ( $1 refers to the first field in the line, for instance), but you can use sed and cut as well.

Читайте также:  Linux permissions to delete

You can use awk ‘NR > 1 ‘ to get the first column (using any whitespace character as a delimiter while skipping the first line) or sed 1d | cut -d$’\t’ -f1 . Note that $’\t’ is bash-specific syntax for a literal tab character, if your file is padded with spaces rather than using tabs to delimit fields, you can’t use the sed . | cut . example.

#!/bin/bash for f in $(tst get files | awk 'NR > 1 ') do echo "FILE :[$]" done 
#!/bin/bash for f in $(tst get files | sed 1d | cut -d$'\t' -f1) do echo "FILE :[$]" done 

to avoid unnecessary splitting in the for loop. It’s best to set IFS to something specific outside the loop body to prevent ‘a file with whitespace.txt’ from being broken up.

OLD_IFS=IFS IFS=$'\n\t' for f in $(tst get files | sed 1d | cut -d$'\t' -f1) do echo "FILE :[$]" done 

Источник

How do you extract a particular column or row from the output of a command?

How would I extract a column from the output, say the second one? And, for example, store it in a variable? Is it possible to do this?

3 Answers 3

That’s exactly what awk was made for. Using the output of xinput on my system:

$ xinput --list --short ⎡ Virtual core pointer [master pointer (3)] ⎜ ↳ Virtual core XTEST pointer [slave pointer (2)] ⎜ ↳ SIGMACHIP USB Keyboard [slave pointer (2)] ⎜ ↳ Logitech M325 [slave pointer (2)] ⎜ ↳ AlpsPS/2 ALPS DualPoint Stick [slave pointer (2)] ⎜ ↳ AlpsPS/2 ALPS DualPoint TouchPad [slave pointer (2)] ⎣ Virtual core keyboard [master keyboard (2)] ↳ Virtual core XTEST keyboard [slave keyboard (3)] ↳ Power Button [slave keyboard (3)] ↳ Video Bus [slave keyboard (3)] ↳ Power Button [slave keyboard (3)] ↳ SIGMACHIP USB Keyboard [slave keyboard (3)] ↳ Dell WMI hotkeys [slave keyboard (3)] ↳ AT Translated Set 2 keyboard [slave keyboard (3)] ↳ Sleep Button [slave keyboard (3)] ↳ UVC Camera (046d:0819) [slave keyboard (3)] $ xinput --list --short | awk -F"\t" '' id=2 id=4 id=12 id=14 id=17 id=16 id=3 id=5 id=6 id=7 id=8 id=11 id=18 id=13 id=9 id=10 

awk will automatically split each input line into fields on the value given by -F . By default, it splits on whitespace, but here we need a tab. The 1st field is $1 , the second $2 etc.

Alternatively, you can use cut :

$ xinput --list --short | cut -f2 id=2 id=4 id=12 id=14 id=17 id=16 id=3 id=5 id=6 id=7 id=8 id=11 id=18 id=13 id=9 id=10 

You can save the output in a shell variable just like you would for any other command:

$ ids=$(xinput --list --short | awk -F"\t" '') $ echo $ids id=2 echo "$ids" id=2 id=4 id=12 id=14 id=17 id=16 id=3 id=5 id=6 id=7 id=8 id=11 id=18 id=13 id=9 id=10 

You could also save it as an array:

$ ids=( $(xinput --list --short | awk -F"\t" '') ) $ echo $ id=2 echo $ id=12 

Источник

Оцените статью
Adblock
detector