help Unix and Awk scripts


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
help Unix and Awk scripts

consider a file with the following format: a header row with

colon-separated field names, and data rows with colon-separated values. for

example:



AGE:EYES:HAIR

32:BLUE:BLONDE

54:BROWN:BROWN



write a script, named extract_columns, that prints out the values

corresponding to the fields specified by the user on the command line, in the

order specified by the user. suppose that the above file was named data.txt.

then typing:



%> extract_columns data.txt EYES



should produce



BLUE

BROWN



and typing:



%> extract_columns data.txt HAIR AGE



should produce



BLONDE 32

BROWN 54



the script should die with a warning if the user requests a field that does

not exist in the data file. The script should run on any input file that

conforms to the format described above.
# 2  
Is this for a homework assignment?
# 3  
try this one

Hi,

Hope this one can help you.

input(a):
Code:
AGE:EYES:HAIR

32:BLUE:BLONDE

54:BROWN:BROWN

code(b):

Code:
nawk -v c="$*" 'BEGIN{FS=":"
}
{
if (NR==1)
        for (i=1;i<=NF;i++)
                if (index(c,$i)!=0)
                        a[i]=i
if (NR>1)
{
        for (j in a)
        printf $j " "
        print ""
}
}' a

output:
Code:
sh b EYES:

BLUE

BROWN

sh b AGE HAIR:

BLONDE 32

BROWN 54

# 4  
Hi, here is one script, bit lengthy one..

#!/bin/sh
i=1
flag=0
IFS=:
for x in `grep "$1" file`
do
if [ "$x" == "$1" ]
then
flag=1
break;
fi
i=`expr $i + 1`
done

if [ $flag -eq 1 ]
then
awk -F':' "{ print $`echo $i` }" file
else
echo "string not available"
exit 1
fi

Note: It has one disadvantage, the grep is case sensitive, so the user should feed the correct case of string to search for in the file.
# 5  
#!/usr/bin/ksh

case $1 in

AGE) cat filename |cut -d: -f1
;;
EYES) cat filename |cut -d: -f2
;;
HAIR) cat filename |cut -d: -f3
;;
esac
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #900
Difficulty: Medium
KornShell (ksh) is a Unix shell which was developed by David Korn at UC Berkeley.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Combine awk scripts

Hi, Below command is working as expected, but would like to know how to club the two AWK scripts in the command into one echo -e "MMS000101S0203430A|20180412E|\nMMB0001INVESTMENT||107-86193-01-03|\nMMB0001FUND||107-86193-04-01|\nMMC9991 " | awk -F'|' -v OFS=, '/^MMC9991/{print r"|"s,t; next}... (3 Replies)
Discussion started by: JSKOBS
3 Replies

2. Shell Programming and Scripting

Merging together two awk scripts

I have two awk scripts shown below. checkTrvt.awk works on file format .xt, whereas checkData.awk workds on file format .dat I want to merge the two scripts together, if I find that the user passed .xt file I do the code for .xt file, whereas if user passes .dat file, I go through the code for... (9 Replies)
Discussion started by: kristinu
9 Replies

3. Shell Programming and Scripting

Combining two awk scripts

I have a file like this consisting of blocks separated by > of two number X and T > 10 0 13 5.92346 16 10.3106 19 13.9672 22 16.9838 25 19.4407 28 21.4705 31 23.1547 34 24.6813 37 26.0695 40 27.3611 43 28.631 46 29.8366 49 30.9858 52 32.0934 55 33.1458 (6 Replies)
Discussion started by: kristinu
6 Replies

4. Shell Programming and Scripting

Unix scripts

hi everyone.Can anyone tell what books to study for unix shell scripting.ive planned to learn unix scripting but i dont kno what kind of books to refer so do help me..Thanks in advance. (1 Reply)
Discussion started by: madanmeer
1 Replies

5. Shell Programming and Scripting

Unix Scripts

Hi, I have to write 2 unix scripts: 1. Check whether the databases are up or down which are on different servers. 2. Check the file system space on different Unix servers. Please share your model scripts here. I have to submit this ASAP. Appreciate your reply...... Thanks (4 Replies)
Discussion started by: dreams5617
4 Replies

6. UNIX for Advanced & Expert Users

Unix Scripts

Hi, I have to write 2 unix scripts: 1. Check whether the databases are up or down which are on different servers. 2. Check the file system space on different Unix servers. Please share your model scripts here. I have to submit this ASAP. Appreciate your reply...... Thanks (1 Reply)
Discussion started by: dreams5617
1 Replies

7. UNIX for Advanced & Expert Users

Porting of Windows written unix scripts to unix platform

Can anybody help me in finding out a solution for the problem below? When we write .unix or .sh files in windows OS and port them to Unix platforms there is a character ^M inserted at the end of each line of the script file. During ftp porting I set the transfer mode as ASCII for the script... (7 Replies)
Discussion started by: tamilselvi
7 Replies

8. UNIX for Dummies Questions & Answers

unix scripts

Hi all Can sombody tell me how can i run a script as a regular user and then change in the middle of the script to root,perform a task that needs root permisions and get back to the regular user. (4 Replies)
Discussion started by: yelalouf
4 Replies

9. UNIX for Advanced & Expert Users

vi scripts on UNIX

Hi there I'm hoping one of the expert unix guys or gals can assist me in understanding the language within scripts e.g clear while true do tput cup 0 0 LIST=`grep cisco /etc/hosts|grep -v _int | awk '{print $2}'` for SITE in $LIST do IP=`grep "$SITE"... (4 Replies)
Discussion started by: nemex
4 Replies

10. UNIX for Dummies Questions & Answers

UNIX Scripts

I need to find a place or places on the Internet where I can find UNIX scripts to view and to modify to make life easy on the UNIX environment. Shell scripts to be used, ksh, sh, csh. Can someone help me on this. Thanks ;) (2 Replies)
Discussion started by: wolf
2 Replies

Featured Tech Videos