How to print column by getting user input.?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to print column by getting user input.?
# 1  
Old 12-08-2012
How to print column by getting user input.?

Hi..

I have data file.. and I could able to print using cat and awk

like this

Code:
cat filename
awk '{print $1 "\t" $2 "\t" $3}' filename

but I want to receive input from user like this

Code:
echo -n " how many columns you want to display : "
read abc

suppose if I give all, it should display all column, and suppose if I give 1,4,5,8,3

it should display only 1st, 4th,5th,8th,3rd column...

How can I do this...

I have one more problem,

I have one script...using for loop for that.. like this
Code:
for file in *.txt ; do
----------
---------
---------
done

if for loop executed successfully, I want to display

Code:
echo -e "Operation successful"

if not I want to display
Code:
echo -e "Check user input,operation unsuccessful"

please go through attached file
# 2  
Old 12-08-2012
You seem to want tab separated output. Is the input tab separated, too? If not, what is the format of your input file?

Is it an error if one or more input lines don't contain a field your want to print?
# 3  
Old 12-08-2012
This looks like you try to reinvent the "cut" program. But "cut" already exists, so it seems rather pointless. Have a look at the man page of cut.

I hope this helps.

bakunin
# 4  
Old 12-09-2012
Actually I just tried to print using tab, my requirement is not tab separated.
I just want to receive input from user that, which column u wants to print..
if user gives input as all it has to display all column,
suppose if user gives input like this.. 1,2,3,5,7 it has to display these columns as given in a order....

---------- Post updated at 01:42 AM ---------- Previous update was at 01:41 AM ----------

Please Mr. Don Cragun have a look on attachment..
# 5  
Old 12-09-2012
Quote:
Originally Posted by Akshay Hegde
Actually I just tried to print using tab, my requirement is not tab separated.
I just want to receive input from user that, which column u wants to print..
if user gives input as all it has to display all column,
suppose if user gives input like this.. 1,2,3,5,7 it has to display these columns as given in a order....

---------- Post updated at 01:42 AM ---------- Previous update was at 01:41 AM ----------

Please Mr. Don Cragun have a look on attachment..
So, if the requirement is not tab separated output fields, what is the requirement for the output field separator?

I looked at your attachment. The last character of the last field on each line is a carriage return. So, any request to print field 8 followed by any other field will overwrite data sent to a video output device. This can easily be seen if you pipe the output through od -c. Will you remove these spurious carriage returns from your input files before feeding them into the script you're requesting, or does the script have to do that too?

Other than the fact that your fields have variable numbers of spaces instead of a single character as the field separator, you haven't said why cut won't do what you need. If you strip off leading spaces on each line and convert other occurrences of one or more spaces to a single space or tab, I don't see why cut won't do what you need.
# 6  
Old 12-09-2012
Let me explain with my requirement

Assume this is my input file

Code:
      1.006   5.488193    27.9850    4.23149     1.7454     0.2259    34.0687      1.000 
      2.012   5.488272    27.9851    4.23844     1.7719     0.2071    34.0689      2.000 
      3.018   5.488225    27.9847    4.25130     1.7509     0.2158    34.0685      3.000 
      4.024   5.488208    27.9851    4.24312     1.7681     0.2201    34.0679      4.000 
      5.030   5.488127    27.9840    4.23286     1.7955     0.2403    34.0677      5.000 
      6.035   5.488064    27.9829    4.24553     1.8051     0.2633    34.0678      6.000

space separated...containing 8 columns,
suppose if user input is all then display all 8 column

if user input is 1,3,4,5,6
then cut or copy 1,3,4,5,6 column from file and display on monitor

Moderator's Comments:
Mod Comment edit by bakunin: Please view this code tag video for how to use code tags when posting code and data.

Last edited by bakunin; 12-09-2012 at 06:47 AM..
# 7  
Old 12-09-2012
Brilliant application of cut!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need awk or Shell script to compare Column-1 of two different CSV files and print if column-1 matche

Example: I have files in below format file 1: zxc,133,joe@example.com cst,222,xyz@example1.com File 2 Contains: hxd hcd jws zxc cst File 1 has 50000 lines and file 2 has around 30000 lines : Expected Output has to be : hxd hcd jws (5 Replies)
Discussion started by: TestPractice
5 Replies

2. Shell Programming and Scripting

Search last column of INPUT.txt in TABLEs text and add correspond columns to INPUT.txt

Hi dears i use bash shell i have INPUT.txt like this number of columns different in one some row have 12 , some 11 columns see last column INPUT.txt CodeGender Age Grade Dialect Session Sentence Start End Length Phonemic Phonetic 63 M 27 BS/BA TEHRANI 3 4 298320 310050... (2 Replies)
Discussion started by: alii
2 Replies

3. Shell Programming and Scripting

Input file needs to match a column and print the entire line

I have a file with class c IP addresses that I need to match to a column and print the matching lines of another file. I started playing with grep -if file01.out file02.out but I am stuck as to how to match it to a column and print the matching lines; cat file01.out 10.150.140... (5 Replies)
Discussion started by: lewk
5 Replies

4. Shell Programming and Scripting

User input and run awk using the input

I am trying to allow a user to enter in text and then store that text in a variable $gene to run in an awk command in which those values are used to run some calculations. I am getting syntax errors however, when I try. Thank you :). The awk runs great if it is a pre-defined file that is used,... (7 Replies)
Discussion started by: cmccabe
7 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. UNIX for Dummies Questions & Answers

print user variable input

this may be basic thing for everyone here, but i cant push awk to print the variable user input which is INS, please help. code: INS=$1 printf '\n' symdg list | grep $INS-clone | awk -v i=$INS '{ID=substr($4,9,4)}{print "Device Group: "$1,"at Array "ID,i}' output: $ ./test.sh patty ... (5 Replies)
Discussion started by: prodigy06
5 Replies

7. Shell Programming and Scripting

How to get the user input recursively until the user provides valid input

Hi, echo "Enter file name of input file list along with absolute path : " read inputFileList if then for string in `cat inputFileList` do echo $string done else echo " file does not exist" fi From the above code, if the user enters a invalid file... (1 Reply)
Discussion started by: i.srini89
1 Replies

8. Shell Programming and Scripting

comparing column of two different files and print the column from in order of 2nd file

Hi friends, My file is like: Second file is : I need to print the rows present in file one, but in order present in second file....I used while read gh;do awk ' $1=="' $gh'" {print >> FILENAME"output"} ' cat listoffirstfile done < secondfile but the output I am... (14 Replies)
Discussion started by: CAch
14 Replies

9. Shell Programming and Scripting

print first few lines, then apply regex on a specific column to print results.

abc.dat tty cpu tin tout us sy wt id 0 0 7 3 19 71 extended device statistics r/s w/s kr/s kw/s wait actv wsvc_t asvc_t %w %b device 0.0 133.2 0.0 682.9 0.0 1.0 0.0 7.2 0 79 c1t0d0 0.2 180.4 0.1 5471.2 3.0 2.8 16.4 15.6 15 52 aaaaaa1-xx I want to skip first 5 line... (4 Replies)
Discussion started by: kchinnam
4 Replies

10. Shell Programming and Scripting

Awk+Grep Input file needs to match a column and print the entire line

I'm having problems since few days ago, and i'm not able to make it works with a simple awk+grep script (or other way to do this). For example, i have a input file1.txt: cat inputfile1.txt 218299910417 1172051195 1172070231 1172073514 1183135117 1183135118 1183135119 1281440202 ... (3 Replies)
Discussion started by: poliver
3 Replies
Login or Register to Ask a Question