How to avoid column with a specific name using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to avoid column with a specific name using awk
# 1  
Old 04-02-2014
How to avoid column with a specific name using awk

how can avoid column names that start with Nx_*?

file1

Code:
name  Nx_8977  Nx_988  NN
a1  2  0  8
b1  2  0  8

ouput

Code:
name  NN
a1  8
b1  8

# 2  
Old 04-02-2014
Something like:
Code:
~/tmp$ cat x.awk
NR==1 {
   for (i=1;i<=NF;i++) {
      if ($i !~ /^Nx_/) {
         output_cols[i]
      }
   }
}
{
   for (i=1;i<=NF;i++) {
      if (i in output_cols) {
         printf $i OFS
      }
   }
   printf "\n"
}
~/tmp$ awk -f x.awk file
name NN
a1 8
b1 8

(Note that this doesn't handle exclusion of all columns very well, and gives an extra field separator on each line)
# 3  
Old 04-02-2014
This version doesn't add an extra field separator at the end of a line and is perfectly happy no matter how many of the fields are rejected. It also uses two spaces as the field separator (matching the input and output samples).
Code:
awk '
NR == 1 {
	for(i = 1; i <= NF; i++)
		if($i !~ /^Nx_/)
			of[++ofc] = i
}
{	for(i = 1; i <= ofc; i++)
		printf("%s%s", $of[i], i == ofc ? "" : "  ")
	print ""
}' file1

which, with the given sample input, produces the output:
Code:
name  NN
a1  8
b1  8

If you want to use this on a Solaris/SunOS system, use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of the default /usr/bin/awk.
These 2 Users Gave Thanks to Don Cragun For This Post:
# 4  
Old 04-02-2014
Quote:
Originally Posted by Don Cragun
and is perfectly happy no matter how many of the fields are rejected.
What I was referring to was that it outputs a blank line if all fields are rejected - which is the same in both our solutions.
# 5  
Old 04-02-2014
Quote:
Originally Posted by CarloM
What I was referring to was that it outputs a blank line if all fields are rejected - which is the same in both our solutions.
Yes. I thought that was the desired behavior. To produce no output if no fields are selected, change the following two lines in my script:
Code:
		printf("%s%s", $of[i], i == ofc ? "" : "  ")
	print ""

to the single line:
Code:
		printf("%s%s", $of[i], i == ofc ? "\n" : "  ")

# 6  
Old 04-02-2014
Longhand using __builtins__ only, OSX 10.7.5, shell and default terminal.
Note that the commented out section uses a tab instead of spaces.
Code:
#!/bin/sh
# cols.sh
echo "name  Nx_8977  Nx_988  NN
a1  2  0  8
b1  2  0  8" > /tmp/filename
temp=""
text=""
while read line
do
	temp=($line)
	text="$text${temp[0]}  ${temp[3]}\n"
	#text="$text${temp[0]}\t${temp[3]}\n"
done < /tmp/filename
printf "$text" > /tmp/newfilename
cat < /tmp/newfilename

Results:-
Code:
Last login: Wed Apr  2 19:06:23 on ttys000
AMIGA:barrywalker~> chmod 755 cols.sh
AMIGA:barrywalker~> ./cols.sh
name  NN
a1  8
b1  8
AMIGA:barrywalker~> _

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using awk to change a specific column and in a specific row

I am trying to change the number in bold to 2400 01,000300032,193631306,190619,0640,1,80,,2/ 02,193631306,000300032,1,190618,0640,CAD,2/ I'm not sure if sed or awk is the answer. I was going to use sed and do a character count up to that point, but that column directly before 0640 might... (8 Replies)
Discussion started by: juggernautjoee
8 Replies

2. Shell Programming and Scripting

sed or awk to remove specific column to one range

I need to remove specific column to one range source file 3 1 000123456 2 2 000123569 3 3 000123564 12 000123156 15 000125648 128 000125648 Output required 3 000123456 2 000123569 3 000123564 12 000123156 15 000125648 128 000125648 (6 Replies)
Discussion started by: ranjancom2000
6 Replies

3. Shell Programming and Scripting

Overwrite specific column in xml file with the specific column from adjacent line

I have an xml file dumped from rrd file, that I want to "patch" so the xml file doesn't contain any blank hole in the resulting graph of the rrd file. Here is the file. <!-- 2015-10-12 14:00:00 WIB / 1444633200 --> <row><v> 4.0419731265e+07 </v><v> 4.5045912770e+06... (2 Replies)
Discussion started by: rk4k
2 Replies

4. Shell Programming and Scripting

awk : search last index in specific column

I am trying to search a given text in a file and find its last occurrence index. The task is to append the searched index in the same file but in a separate column. I am able to accomplish the task partially and looking for a solution. Following is the detailed description: names_file.txt ... (17 Replies)
Discussion started by: tarun.trehan
17 Replies

5. UNIX for Dummies Questions & Answers

Average by specific column value, awk

Hi, I am searching for an awk-script that computes the mean values for the $2 column, but addicted to the values in the $1 column. It also should delete the unnecessary lines after computing... An example (for some reason I cant use the code tag button): cat list.txt 1 10 1 30 1 20... (2 Replies)
Discussion started by: bjoern456
2 Replies

6. Shell Programming and Scripting

Remove Specific Column in a File using awk

Hi, I would like to ask your expertise to remove specific column no. 8 in the below file using but I don't have an idea on how to simply do this using awk command. Appreciate your help in advance. Input f: ABC 1 1XC CDA 1 2YC CCC 1 3XC AVD 1 3XA Expected output file: ABC 1 1C CDA... (9 Replies)
Discussion started by: zzavilz
9 Replies

7. Shell Programming and Scripting

Awk: Need help replacing a specific column in a file by part of a column in another file

Hi, I have two input files as File1 : ABC:client1:project1 XYZ:client2-aa:project2 DEF:client4:proj File2 : client1:W-170:xx client2-aa:WT-04:yy client4:L-005A:zz Also, array of valid values can be hardcoded like Output : ABC:W:project1 XYZ:WT:project2 (1 Reply)
Discussion started by: aa2601
1 Replies

8. UNIX for Dummies Questions & Answers

awk: convert column to row in a specific way

Hi all! I have this kind of output: a1|b1|c1|d1|e1 a2|b2|c2 a3|b3|c3|d3 I would like to transpose columns d and e (when they exist) in column c, and under the row where they come from. Then copying the beginning of the row. In order to obtain: a1|b1|c1 a1|b1|d1 a1|b1|e1 a2|b2|c2... (1 Reply)
Discussion started by: lucasvs
1 Replies

9. Shell Programming and Scripting

Assigning a specific format to a specific column in a text file using awk and printf

Hi, I have the following text file: 8 T1mapping_flip02 ok 128 108 30 1 665000-000008-000001.dcm 9 T1mapping_flip05 ok 128 108 30 1 665000-000009-000001.dcm 10 T1mapping_flip10 ok 128 108 30 1 665000-000010-000001.dcm 11 T1mapping_flip15 ok 128 108 30... (2 Replies)
Discussion started by: goodbenito
2 Replies

10. Shell Programming and Scripting

Insert a text from a specific row into a specific column using SED or AWK

Hi, I am having trouble converting a text file. I have been working for this whole day now, still i couldn't make it. Here is how the text file looks: _______________________________________________________ DEVICE STATUS INFORMATION FOR LOCATION 1: OPER STATES: Disabled E:Enabled ... (5 Replies)
Discussion started by: Issemael
5 Replies
Login or Register to Ask a Question