Awk, ouptut columns in a file by column name


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Awk, ouptut columns in a file by column name
# 15  
Old 01-10-2012
Tools

I created a new shell script that will take the delimiters, data file and column names as parameters and does validation before outputting the results

Example:
query_by_column_name '|' ',' datafile1.txt ADDRS FNAME CITY LNAME

I attached the script too as the code below loses the formatting

Code:
#!/usr/bin/ksh
#------------------------------------------------------------------------------------
# Program: query_by_column_name
# Author: Srinivas Y.
# This program takes filename, the delimiter, the column names to be output and the desired output delimiter
# and prints the output in that format
#------------------------------------------------------------------------------------

if [ $# -lt 4 ]
then
  echo "Invalid number of arguments" 1>&2
  echo "Usage: b_column_name <input delimiter> <output delimiter> <filename> <col1> <col2> ...."  1>&2
  exit 1
fi

INP_DLMTR=$1
OUT_DLMTR=$2
INP_FILE_NM=$3
POSLIST=""
COUNT=""

#echo "Input Delimiter : ${INP_DLMTR}"
#echo "output Delimiter: ${OUT_DLMTR}"
#echo "Input File Name : ${INP_FILE_NM}"

shift 3;

while (($#))
do

  COL_NM=$1
  #echo "Column name : ${COL_NM}"

  head -1 ${INP_FILE_NM} | tr ${INP_DLMTR} '\n' | grep -n "^${COL_NM}$" > /tmp/null
  RC=$?
  if [ $RC -ne 0 ]
  then
     echo "Column ${COL_NM} is not found. Aborting ..." 1>&2
     exit $RC
  fi

  COUNT=`head -1 ${INP_FILE_NM} | tr ${INP_DLMTR} '\n' | grep -n "^${COL_NM}$" | wc -l`
  if [ $COUNT -gt 1 ]
  then
     echo "Multiple columns with name ${COL_NM} found. Aborting ..." 1>&2
     exit 1
  fi

  FLD=`head -1 ${INP_FILE_NM} | tr ${INP_DLMTR} '\n' | grep -n "^${COL_NM}$" | cut -d ':' -f 1`
  if [ ! -n "$POSLIST" ]
  then
    POSLIST='$'"$FLD"
  else
    POSLIST="$POSLIST OFS "'$'"$FLD"
  fi
  
  #echo "POSLIST: $POSLIST"
  
  shift
done

echo "awk -F '${INP_DLMTR}' '{print $POSLIST}' OFS='${OUT_DLMTR}' ${INP_FILE_NM}"| sh
RC=$?

exit $RC

Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by ysrini; 01-10-2012 at 12:05 PM.. Reason: updated script
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Using awk to split a column into two columns

Hi, I am trying to split the following output into two columns, where each column has Source: Destination: OUTPUT TO FILTER $ tshark -r Capture_without_mtr.pcap -V | awk '/ (Source|Destination): /' | more Source: x.x.x.x Destination: x.x.x.x Source:... (2 Replies)
Discussion started by: sand1234
2 Replies

2. Shell Programming and Scripting

awk split columns to row after N number of column

I want to split this with every 5 or 50 depend on how much data the file will have. And remove the comma on the end Source file will have 001,0002,0003,004,005,0006,0007,007A,007B,007C,007E,007F,008A,008C Need Output from every 5 tab and remove the comma from end of each row ... (4 Replies)
Discussion started by: ranjancom2000
4 Replies

3. Shell Programming and Scripting

awk add all columns if column 1 name matches

Hi - I want to add all columns if column1 name matches. TOPIC1 5 1 4 TOPIC2 3 2 1 TOPIC3 7 2 5 TOPIC1 6 3 3 TOPIC2 4 1 3 TOPIC3 9 5 4 . . . . . . . . . . . . Result should look like TOPIC1 11 4 7 TOPIC2 7 3 4 (1 Reply)
Discussion started by: oraclermanpt
1 Replies

4. Shell Programming and Scripting

awk to Sum columns when other column has duplicates and append one column value to another with Care

Hi Experts, Please bear with me, i need help I am learning AWk and stuck up in one issue. First point : I want to sum up column value for column 7, 9, 11,13 and column15 if rows in column 5 are duplicates.No action to be taken for rows where value in column 5 is unique. Second point : For... (1 Reply)
Discussion started by: as7951
1 Replies

5. UNIX for Advanced & Expert Users

Conversion of rows to columns using awk based om column value

HI, My Input file data is dn:adcfgeneral id:13343 Name:xxxxxx Password:iutyerwuitywue wpuwt tuiytruityrutyrwtyrwp dn:cdferwjyyyy id:3875 Name:yyyy Password :hgfdsjkfhdsfkdlshf dshfkldshfdklsfh interset:uiuiufj My output should be ... (6 Replies)
Discussion started by: dineshaila
6 Replies

6. Shell Programming and Scripting

Need help with awk statement to break nth column in csv file into 3 separate columns

Hello Members, I have a csv file in the format below. Need help with awk statement to break nth column into 3 separate columns and export the changes to new file. input file --> file.csv cat file.csv|less "product/fruit/mango","location/asia/india","type/alphonso" need output in... (2 Replies)
Discussion started by: awk-admirer
2 Replies

7. Shell Programming and Scripting

Please Help!!!! Awk for summing columns based on selected column value

a,b,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,bb,cc,dd,ee,ff,gg,hh,ii a thru ii are digits and strings.... The awk needed....if coloumn 9 == i (coloumn 9 is string ), output the sum of x's(coloumn 22 ) in all records and sum of y's (coloumn 23 ) in all records in a file (records.txt).... (6 Replies)
Discussion started by: BrownBob
6 Replies

8. Shell Programming and Scripting

help sum columns by break in first column with awk or sed or something.

I have some data that is something like this? item: onhand counted location ITEM0001 1 0 a1 ITEM0001 0 1 a2 ITEM0002 5 0 b5 ITEM0002 0 6 c1 I want to sum up... (6 Replies)
Discussion started by: syadnom
6 Replies

9. Shell Programming and Scripting

Single column to multiple columns in awk

Hi - I'm new to the awk programming language. I'm trying to print a single column of data to several columns, and I found an article on iTWorld.com (ITworld.com - Printing in columns). It looks like the mkCols2 script is very close to what I need to do, but it looks like the end of the code... (2 Replies)
Discussion started by: astroDave
2 Replies

10. Shell Programming and Scripting

How to check Null values in a file column by column if columns are Not NULLs

Hi All, I have a table with 10 columns. Some columns(2nd,4th,5th,7th,8th and 10th) are Not Null columns. I'll get a tab-delimited file and want to check col by col and generate seperate error code for each col eg:102 if 2nd col value is NULL and 104 if 4th col value is NULL so on... I am a... (7 Replies)
Discussion started by: Mandab
7 Replies
Login or Register to Ask a Question