How to get specific words by index in a line bash
up vote
0
down vote
favorite
I have a script that takes parameters such as:
script.sh 1 3
I want to then go through a text file and print out the first and third words from each line. I have simply no idea how to do this. If anyone could help I'd really appreciate it...
This is what I currently have:
counter=0
wordcounter=0
param=$(echo "$3" | tr , " ")
words()
for word in $1; do
for col in $param; do
if [ $wordcounter -eq $col ]; then
echo $word
fi
done
done
wordcounter=$((wordcounter + 1))
eachline() tr , " ")
for word in $newline; do
if [ $counter -gt 3 ]; then
echo "$word"
fi
counter=$((counter + 1))
done
if [ $counter -gt 0 ]; then
words "$newline"
fi
counter=$((counter + 1))
while read line; do
eachline $line
done < company/employee.txt
bash shell text
add a comment |
up vote
0
down vote
favorite
I have a script that takes parameters such as:
script.sh 1 3
I want to then go through a text file and print out the first and third words from each line. I have simply no idea how to do this. If anyone could help I'd really appreciate it...
This is what I currently have:
counter=0
wordcounter=0
param=$(echo "$3" | tr , " ")
words()
for word in $1; do
for col in $param; do
if [ $wordcounter -eq $col ]; then
echo $word
fi
done
done
wordcounter=$((wordcounter + 1))
eachline() tr , " ")
for word in $newline; do
if [ $counter -gt 3 ]; then
echo "$word"
fi
counter=$((counter + 1))
done
if [ $counter -gt 0 ]; then
words "$newline"
fi
counter=$((counter + 1))
while read line; do
eachline $line
done < company/employee.txt
bash shell text
what have you tried so far?
– oguzismail
Nov 10 at 12:40
I'm currently trying to make a second script that takes a line as parameter, then running each line from the file into the script.
– Raph117
Nov 10 at 12:42
In the script itself I'm setting a counter and ignoring the number of words in the first line
– Raph117
Nov 10 at 12:42
It's very very very hacky
– Raph117
Nov 10 at 12:42
2
Willcut -d " " -f 1,3 file
do?
– James Brown
Nov 10 at 13:27
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a script that takes parameters such as:
script.sh 1 3
I want to then go through a text file and print out the first and third words from each line. I have simply no idea how to do this. If anyone could help I'd really appreciate it...
This is what I currently have:
counter=0
wordcounter=0
param=$(echo "$3" | tr , " ")
words()
for word in $1; do
for col in $param; do
if [ $wordcounter -eq $col ]; then
echo $word
fi
done
done
wordcounter=$((wordcounter + 1))
eachline() tr , " ")
for word in $newline; do
if [ $counter -gt 3 ]; then
echo "$word"
fi
counter=$((counter + 1))
done
if [ $counter -gt 0 ]; then
words "$newline"
fi
counter=$((counter + 1))
while read line; do
eachline $line
done < company/employee.txt
bash shell text
I have a script that takes parameters such as:
script.sh 1 3
I want to then go through a text file and print out the first and third words from each line. I have simply no idea how to do this. If anyone could help I'd really appreciate it...
This is what I currently have:
counter=0
wordcounter=0
param=$(echo "$3" | tr , " ")
words()
for word in $1; do
for col in $param; do
if [ $wordcounter -eq $col ]; then
echo $word
fi
done
done
wordcounter=$((wordcounter + 1))
eachline() tr , " ")
for word in $newline; do
if [ $counter -gt 3 ]; then
echo "$word"
fi
counter=$((counter + 1))
done
if [ $counter -gt 0 ]; then
words "$newline"
fi
counter=$((counter + 1))
while read line; do
eachline $line
done < company/employee.txt
bash shell text
bash shell text
edited Nov 10 at 12:57
asked Nov 10 at 12:11
Raph117
152110
152110
what have you tried so far?
– oguzismail
Nov 10 at 12:40
I'm currently trying to make a second script that takes a line as parameter, then running each line from the file into the script.
– Raph117
Nov 10 at 12:42
In the script itself I'm setting a counter and ignoring the number of words in the first line
– Raph117
Nov 10 at 12:42
It's very very very hacky
– Raph117
Nov 10 at 12:42
2
Willcut -d " " -f 1,3 file
do?
– James Brown
Nov 10 at 13:27
add a comment |
what have you tried so far?
– oguzismail
Nov 10 at 12:40
I'm currently trying to make a second script that takes a line as parameter, then running each line from the file into the script.
– Raph117
Nov 10 at 12:42
In the script itself I'm setting a counter and ignoring the number of words in the first line
– Raph117
Nov 10 at 12:42
It's very very very hacky
– Raph117
Nov 10 at 12:42
2
Willcut -d " " -f 1,3 file
do?
– James Brown
Nov 10 at 13:27
what have you tried so far?
– oguzismail
Nov 10 at 12:40
what have you tried so far?
– oguzismail
Nov 10 at 12:40
I'm currently trying to make a second script that takes a line as parameter, then running each line from the file into the script.
– Raph117
Nov 10 at 12:42
I'm currently trying to make a second script that takes a line as parameter, then running each line from the file into the script.
– Raph117
Nov 10 at 12:42
In the script itself I'm setting a counter and ignoring the number of words in the first line
– Raph117
Nov 10 at 12:42
In the script itself I'm setting a counter and ignoring the number of words in the first line
– Raph117
Nov 10 at 12:42
It's very very very hacky
– Raph117
Nov 10 at 12:42
It's very very very hacky
– Raph117
Nov 10 at 12:42
2
2
Will
cut -d " " -f 1,3 file
do?– James Brown
Nov 10 at 13:27
Will
cut -d " " -f 1,3 file
do?– James Brown
Nov 10 at 13:27
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
Use awk
:
$ awk 'print $1 " " $3' file
for file
:
1 2 3 4 5
6 7 8 9 0
Output is:
1 3
6 8
In bash script:
#!/bin/bash
awk_command="print";
for i in "$@"; do
awk_command="$awk_command $$i " "";
done
awk_command="$awk_command";
awk "$awk_command" file
With this script you can pass any number of indexes:
For 1 and 2:
$ ./script.sh 1 2
1 2
6 7
For 1, 2 and 5:
$ ./script.sh 1 2 5
1 2 5
6 7 0
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
Use awk
:
$ awk 'print $1 " " $3' file
for file
:
1 2 3 4 5
6 7 8 9 0
Output is:
1 3
6 8
In bash script:
#!/bin/bash
awk_command="print";
for i in "$@"; do
awk_command="$awk_command $$i " "";
done
awk_command="$awk_command";
awk "$awk_command" file
With this script you can pass any number of indexes:
For 1 and 2:
$ ./script.sh 1 2
1 2
6 7
For 1, 2 and 5:
$ ./script.sh 1 2 5
1 2 5
6 7 0
add a comment |
up vote
3
down vote
Use awk
:
$ awk 'print $1 " " $3' file
for file
:
1 2 3 4 5
6 7 8 9 0
Output is:
1 3
6 8
In bash script:
#!/bin/bash
awk_command="print";
for i in "$@"; do
awk_command="$awk_command $$i " "";
done
awk_command="$awk_command";
awk "$awk_command" file
With this script you can pass any number of indexes:
For 1 and 2:
$ ./script.sh 1 2
1 2
6 7
For 1, 2 and 5:
$ ./script.sh 1 2 5
1 2 5
6 7 0
add a comment |
up vote
3
down vote
up vote
3
down vote
Use awk
:
$ awk 'print $1 " " $3' file
for file
:
1 2 3 4 5
6 7 8 9 0
Output is:
1 3
6 8
In bash script:
#!/bin/bash
awk_command="print";
for i in "$@"; do
awk_command="$awk_command $$i " "";
done
awk_command="$awk_command";
awk "$awk_command" file
With this script you can pass any number of indexes:
For 1 and 2:
$ ./script.sh 1 2
1 2
6 7
For 1, 2 and 5:
$ ./script.sh 1 2 5
1 2 5
6 7 0
Use awk
:
$ awk 'print $1 " " $3' file
for file
:
1 2 3 4 5
6 7 8 9 0
Output is:
1 3
6 8
In bash script:
#!/bin/bash
awk_command="print";
for i in "$@"; do
awk_command="$awk_command $$i " "";
done
awk_command="$awk_command";
awk "$awk_command" file
With this script you can pass any number of indexes:
For 1 and 2:
$ ./script.sh 1 2
1 2
6 7
For 1, 2 and 5:
$ ./script.sh 1 2 5
1 2 5
6 7 0
answered Nov 10 at 12:39
caco3
7211415
7211415
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238821%2fhow-to-get-specific-words-by-index-in-a-line-bash%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
what have you tried so far?
– oguzismail
Nov 10 at 12:40
I'm currently trying to make a second script that takes a line as parameter, then running each line from the file into the script.
– Raph117
Nov 10 at 12:42
In the script itself I'm setting a counter and ignoring the number of words in the first line
– Raph117
Nov 10 at 12:42
It's very very very hacky
– Raph117
Nov 10 at 12:42
2
Will
cut -d " " -f 1,3 file
do?– James Brown
Nov 10 at 13:27