-1

For example if I have a file with multiple rows and columns is it possible to pull specific rows and columns?

for example

1 4/8/2016 4/7/2016 4/6/2016
2 john_doe jane_doe sarah_test
3 26 45 20
4 4/8/2015 6/8/2016 8/26/2016
5 5:15 2:30 6:00

Desired Output

1 4/8/2016 4/7/2016 4/6/2016
2 john_doe jane_doe sarah_test
4 4/8/2015 6/8/2016 8/26/2016
6
  • what is the selection criteria for rows and columns?
    – MelBurslan
    Apr 9, 2016 at 5:48
  • No criteria other than they are the requested rows. I've used awk 'NR == 1, NR == 3' but that will print 1-3 and not allow further arguments.
    – Vithar91
    Apr 9, 2016 at 5:49
  • That doesn't answer @MelBurslan's question. What criteria makes them "selected rows"? I had to guess for my answer. and that guess is likely to be wrong, or only work for the sample you provided but not for your real data.
    – cas
    Apr 9, 2016 at 5:51
  • There is no pattern that can match those 3 lines of output and ONLY those 3 lines, so the only way to do is by specifying which line numbers are to be printed (lines 1, 2, and 4). BTW, your date format is ambiguous, you should use the ISO standard of YYYY-MM-DD.
    – cas
    Apr 9, 2016 at 6:04
  • Are the numbers at the start of each of your input lines actually part of the input or are you trying to indicate line numbers? Are you trying to select specific line numbers? Please edit your question to make this absolutely clear otherwise you are just wasting everyone's time.
    – Lqueryvg
    Apr 9, 2016 at 6:05

2 Answers 2

2

Yes, it is possible to pull specific rows and columns.

This concludes answering the exact question you asked.


However, let me offer some general advice:

awk is built on a simple condition—action model.

Provide a condition and then provide an action to go with that condition.

Let's say the condition is "third row" and the action is "print the first two fields of that row." This would look like:

awk 'NR==3 {print $1, $2}'

Let's say the condition is "Row contains the letter 'a'" and the action is "Print the whole row."

awk '/a/ {print}'

Since printing the whole row is such a common action, it is the default action:

awk '/a/'

Let's say the condition is "every row" and the action is "print the second field". You can omit the condition:

awk '{print $2}'

For more, read a proper tutorial.

3
  • If I am reading this correctly i would want to use awk 'NR==3 {print $1, $2}' and just brute force the rest in a script with all the rows wanted.
    – Vithar91
    Apr 9, 2016 at 6:04
  • @Vithar91, well for the output you specified, you could use awk 'NR==1;NR==2;NR==4' but you might as well just use sed: sed -e 3d -e 4q filename or sed -n '1p;2p;4p'. There is a tutorial for sed as well. If you're just selecting lines, you don't need awk (use sed instead), but your "brute force" job, whatever it is, can very definitely be done in awk. sed is the assembly language of text processing; awk is the command line spreadsheet.
    – Wildcard
    Apr 9, 2016 at 6:16
  • (Reposted comment to fix link.)
    – Wildcard
    Apr 9, 2016 at 6:17
0
awk '$2 ~ /^[abd]$/ { print $2, $3, $4 }'

for lines where the second field is a, b, or d, print the second, third, and fourth fields.

You weren't at all specific on your selection criteria so I had to guess based on the difference between the input and the output you provided.

4
  • My apologies for not being more clear the columns can contain numbers, dates, names, anything really as this file has a lot of information
    – Vithar91
    Apr 9, 2016 at 5:52
  • That still isn't helpful information. Apr 9, 2016 at 5:53
  • if you expect anyone to be able to provide a better answer, you need to provide better, more detailed information. we can't magically see your real data or read your mind to know what it is you want.
    – cas
    Apr 9, 2016 at 5:53
  • but my answer is a specific example of a generic answer that can be adapted to many situations: if the current line (or a field in the current line) matches a regular expression, then print some or all of the fields.
    – cas
    Apr 9, 2016 at 5:56

Not the answer you're looking for? Browse other questions tagged .