Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Simple question, having a ls -l command it makes a list on the command prompt and if we want to manipulate it we pipeline it with awk command, for instance if we want to to show specific files.

Is that possible without using the awk command but instead using a shell script, if so what would be the column variables how would we manipulate that on fly list do you have also any suggestions on educative links or books.


EDIT:for example a ls -l|awk '{print $1}' the $1 is referring to the first column,is there any way to do that with a shell script variable, for instance if we would like to run the ls -l command inside a shell script and print the exact filenames of which we are searching for. How to do this? Sorry for the misconception I am new to this.

share|improve this question

closed as too broad by jasonwryan, Anthon, slm, rahmu, terdon Dec 9 '13 at 22:44

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

    
Please edit your question to add an example of what you are describing. I think you are asking how you can manipulate text in columns through a shell script, is that so? –  terdon Dec 9 '13 at 14:50
1  
You should not parse the output of ls if you have filenames with spaces and/or newlines there is no way you parse the output. –  Anthon Dec 9 '13 at 14:50

2 Answers 2

Your question right now is not so clear, but I'll have a stab at one of the things I think you are referring to.

If you want to write a script or program that can be used in a pipeline, it must read from standard input and write to standard output. For example, a bash script that adds line numbers:

#!/bin/bash

index=1
while read line; do
    echo $index". "$line
    index=$(($index + 1))
done

If this is called eg.sh you could use it:

ls -1 | ./eg.sh
cat ./eg.sh | ./eg.sh | grep "^5\."

The last one should print out just:

5. echo $index". "$line

The key to getting the input is the read built-in (here's some details).

share|improve this answer
    
I 've done some research and I want to ask,in awk command $1,$2 e.t.c. are referring to the file's or the command's columns, which are the equivalent variables in shell script for example in a loop "read number name" it reads the first variable of each line of a file how we read the others "columns" of that file separetely?is there any any way or awk is mandatory? –  Kosta Dec 9 '13 at 19:27

I am not sure I understand your question, but in the simplest case, where you have space-separated columns of text and no spaces within fields, you could do:

echo "col1 col2 col3 col4" | while read one two three four; do echo "$two"; done

The while read [variable names] shell loop will automatically split its input at whitespace into as many variables as you give.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.