awk - Print variable number of colums from a starting column

Thread Tools Search this Thread
Operating Systems Solaris awk - Print variable number of colums from a starting column
# 1  
Old 09-28-2011
awk - Print variable number of colums from a starting column

Hi guys,

I usualy am able to google awk stuff but I can't find it so far and there are so many awking gurus here that I will give it a shot.

I want to print $1;$3;"$5 up to the $NF". In other words, I can have 1000 colums, but need to have $5 up to the end.

I started with the idea of "removing" the unwanted colums but the thing is that I need to have a field seperator (Smilie between $1 $3 and $5.

Original data looks like:

randomuser UID default_group GID Name of the user

And I would like it to look like:

randomuser;default_group;Name of the user

I still hate SOX audits ... I would do it manually, but we're talking about 50ish servers and 50ish groups ...

Any good ideas?

---------- Post updated at 11:03 AM ---------- Previous update was at 10:58 AM ----------

Nevermind ...

my_stupid_command | while read a b c d e; do echo $a";"$c";"$e;

It works just fine Smilie
# 2  
Old 09-28-2011
you can even save 2 variables

your_command | while read a b b c c; do echo $a";"$b";"$c; done

also :
your_command | sed 's/;[^;]*;/;/2;s/;[^;]*;/;/'

your_command | awk -F\; '{sub($2 FS $3 FS $4,$3)}1'

Last edited by ctsgnb; 09-28-2011 at 01:02 PM..
This User Gave Thanks to ctsgnb For This Post:
# 3  
Old 09-28-2011

That's why I asked Smilie

Too many ways to do the same thing
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print column number while ignoring alpha characters

I have the following script that will print column 4 ("25") when column 1 contains "123". However, I need to ignore the alpha characters that are contained in the input file. If I were to ignore the characters my output would be column 3. What is the best way to print my column of interest... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

2. Shell Programming and Scripting

awk - Print column number that return value comes from

I have the following awk script that I am using to find the max value in the file and print results. awk 'BEGIN {MAX=-1E100} {for (x=2; x<=NF; x++) if ($x>MAX) {MAX = $x; C1 = $1}} END {print substr(C1,1,11), substr(C1,13,4), substr(C1,18,2), MAX}' ABC* Input (ABC*) ... (6 Replies)
Discussion started by: ncwxpanther
6 Replies

3. Shell Programming and Scripting

Pass column number as variable to awk and compare with a string.

Hi All, I have a file test.txt. Content of test.txt : 1 vinay se 2 kumar sse 4 kishore tl I am extracting the content of file with below command. awk '$2 ~ "vinay" {print $0}' test.txt Now instead of hardcoding $2 is there any way pass $2 as variable and compare with a... (7 Replies)
Discussion started by: Girish19
7 Replies

4. Shell Programming and Scripting

Help with compare two column and print out column with smallest number

Input file : 5 20 500 2 20 41 41 0 23 1 Desired output : 5 2 20 0 1 By comparing column 1 and 2 in each line, I hope can print out the column with smallest number. I did try the following code, but it don't look good :( (2 Replies)
Discussion started by: perl_beginner
2 Replies

5. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

6. Shell Programming and Scripting

Using awk to print line starting with particular word

Hi Geeks, Consider this line: driver=c:\folder1\folder2 The above line is contained in a variable say 'var' . I want to copy everything after 'driver=' in to another variable say var2. Please tell me how can this be done. (8 Replies)
Discussion started by: ajincoep
8 Replies

7. Shell Programming and Scripting

Need to print last column based on the value of first 2 colums

Hi Everybody. I need an urgent help, it would be great if somebody will help me out in this regard. See below sample file 1525 805 26 2036 219644. 2598293. 1525 805 126 2327 1525 805 226 ... (6 Replies)
Discussion started by: syahmed
6 Replies

8. Shell Programming and Scripting

awk if statement matching all lines starting w/ a number

Does awk have a syntax for a range of numbers or is this the best way? if ($1 >= 0 && $1 <= 9 ) (7 Replies)
Discussion started by: Arsenalman
7 Replies

9. Shell Programming and Scripting

How to print the number of lines from a file, the starting string should be passed`

Hi , I have file, which has the below content: line 100 a b c d line300 a s d f s line200 a s d a (3 Replies)
Discussion started by: little_wonder
3 Replies

10. Shell Programming and Scripting

can awk print column using a variable ??

i want to print the column file using awk or cut in dynamic manner like trmp=2;temp1=1;temp3=2 awk 'BEGIN{OFS=IFS="\t"} {print $temp,$temp1,$temp3}' client_data.txt or cut -f $temp1,$temp2,$temp3 -d"\t" file_name . but it is showing error , In awk can i use variable as in printing... (36 Replies)
Discussion started by: jambesh
36 Replies
Login or Register to Ask a Question