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.?
# 8  
Old 12-09-2012
Quote:
Originally Posted by Akshay Hegde
Let me explain with my requirement

Assume this is my input file

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
So you have thrown away the carriage return. That is good. But despite the fact that you didn't use code tags, it looks now you have a trailing space instead of a trailing carriage return. And you still have varying numbers of spaces as the field delimiter and you haven't specified what the output field separator is supposed to be.

What output field separator do you want? (Do we have to count the number of spaces at the start of each field and maintain that set of spaces as part of the field when it is printed?)

Why isn't cut sufficient for what you need?
# 9  
Old 12-09-2012
Quote:
Originally Posted by Akshay Hegde
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
To use your own example:

Code:
cut -f1,3,4,5,6 inputfile

will do exactly that. Here is a list of suggestions of "cut" in this thread:

Don Cragun: 2 times
bakunin: 2 times
RudiC: 1 time

How often are you going to ignore what so many long-standing experts tell you to do? I won't embarass you with the question if this is homework - instead I'd like to know if this is a game, Ignore-The-Advice.

Btw., it is possible to use the user input directly, as a variable content, instead of the fixed value i used in your example. But explaining this to you at the rate of ignores-per-answer you show would be more work than i have time for.

I hope this helps.

bakunin
# 10  
Old 12-09-2012
Pls..Mr. RudiC go through my 1st post..
for file in *.txt ; do ---------- --------- --------- done
if for loop executed successfully, I want to display


Code:
Code:
echo -e "Operation successful"
 if not I want to display

Code:
Code:
echo -e "Check user input,operation unsuccessful"

How can I do this..if there is any syntax or fatal error I would like to display
"Check user input,operation unsuccessful"

---------- Post updated at 06:40 AM ---------- Previous update was at 06:34 AM ----------

I understood the suggestion given by the experts regarding the usage of cut, but how can I use in my script, I was just waiting for how to apply if user input was given

Code:
one is using if statement

like this

if input is all

then 

cat filename

if input is 1,2,3,6,5

how can I use cut command ?

# 11  
Old 12-09-2012
Quote:
Originally Posted by Akshay Hegde
Pls..Mr. RudiC go through my 1st post..
. . .
echo -e "Operation successful"
if not I want to display
echo -e "Check user input,operation unsuccessful" . . .
How can I do this..if there is any syntax or fatal error I would like to display "Check user input,operation unsuccessful"
This depends on your shell. Using bash, you might want to consider setting the -e option, resulting in a subshell to exit immediately on error, or trapping the ERR condition. Pls refer to bash man page

Quote:
how can I use cut command ?
By exactly copying the command bakunin has given, or, better, by reading the man page and doing some trial an error.
# 12  
Old 12-09-2012
It seems there is a slight misunderstanding here and i will take the opportunity to clear it up: we are neither an online helpdesk nor do we do consultancy. We do not do other peoples work for them. What we do is to help people help themselves.

There is no way around reading man pages, learning to program in shell and generally learning the tools of the trade. We can help you with details you don't know, but we can't teach you the basics of the job over the net. To understand how to use "$1", for instance, i suggest you either take a class in basic shell scripting or get some book "shell scripting for beginners" and start to read. We have done the same at some point in history and i am sure whatever i was able to grasp will be a piece of cake for you once you direct your energy at it.

As i see it this thread is leading nowhere. The questions are answered and the only thing missing is you actually trying what we told you. We don't expect you to get it right the first time. We are fully well prepared to answer follow-up questions like OK, i have read the man page of "cut", tried <some faulty command follows> and it gave me not the expected result, but only <this> garbage and the error message <message>. What have i done wrong?

From the utter lack of such questions i deduce you haven't even tried. Instead you kept repeating the same question over and over again waiting until someone will write you a complete script. This will never happen here.

Moderator's Comments:
Mod Comment Thread closed.


bakunin
These 4 Users Gave Thanks to bakunin For This Post:
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