Retrieve Field Rows from a Pipe Delimited File
up vote
0
down vote
favorite
I need to get first 9 words from a pipe delimited file and then next 9 words. Any help is appreciated.
cat a.txt
a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|
cat new.ksh
#! /bin/ksh
a=`awk -F "|" ' print NF-1 ' a.txt`
echo $a
Expected Output:
a|b|c|d|e|f|g|h|i|
j|k|l|m|n|o|p|q|r|
s|t|u|v|w|x|y|z
bash shell unix
add a comment |
up vote
0
down vote
favorite
I need to get first 9 words from a pipe delimited file and then next 9 words. Any help is appreciated.
cat a.txt
a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|
cat new.ksh
#! /bin/ksh
a=`awk -F "|" ' print NF-1 ' a.txt`
echo $a
Expected Output:
a|b|c|d|e|f|g|h|i|
j|k|l|m|n|o|p|q|r|
s|t|u|v|w|x|y|z
bash shell unix
What was the reasoning behind usingprint NF-1
? How does it correlate with your expected output?
– hek2mgl
Nov 9 at 17:40
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I need to get first 9 words from a pipe delimited file and then next 9 words. Any help is appreciated.
cat a.txt
a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|
cat new.ksh
#! /bin/ksh
a=`awk -F "|" ' print NF-1 ' a.txt`
echo $a
Expected Output:
a|b|c|d|e|f|g|h|i|
j|k|l|m|n|o|p|q|r|
s|t|u|v|w|x|y|z
bash shell unix
I need to get first 9 words from a pipe delimited file and then next 9 words. Any help is appreciated.
cat a.txt
a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|
cat new.ksh
#! /bin/ksh
a=`awk -F "|" ' print NF-1 ' a.txt`
echo $a
Expected Output:
a|b|c|d|e|f|g|h|i|
j|k|l|m|n|o|p|q|r|
s|t|u|v|w|x|y|z
bash shell unix
bash shell unix
edited Nov 9 at 17:10
asked Nov 9 at 16:48
Josh
417
417
What was the reasoning behind usingprint NF-1
? How does it correlate with your expected output?
– hek2mgl
Nov 9 at 17:40
add a comment |
What was the reasoning behind usingprint NF-1
? How does it correlate with your expected output?
– hek2mgl
Nov 9 at 17:40
What was the reasoning behind using
print NF-1
? How does it correlate with your expected output?– hek2mgl
Nov 9 at 17:40
What was the reasoning behind using
print NF-1
? How does it correlate with your expected output?– hek2mgl
Nov 9 at 17:40
add a comment |
3 Answers
3
active
oldest
votes
up vote
2
down vote
accepted
sed -r 's/([^|]*|)9/&n/g' a.txt
Explanation:[^|]
Not a |.[^|]*|
One field (without |
), followed by a |
.([^|]*|)9
9 fields, each followed by a |
.&n
Replace by the matched string followed by a newline.
thanks for your response. I will give it a shot
– Josh
Nov 10 at 0:50
Very nice elegant solution. Minor note, it gives a trailing bar (|
) that was not in the "expected" output but if the trailing bar is left off the input, it doesn't appear in the output.
– user1683793
Nov 10 at 1:30
add a comment |
up vote
1
down vote
Try this:
mayankp@mayank:~/$ cat a.txt| tr "|" "n" |head -9 | tr "n" "|" > output.txt
mayankp@mayank:~/$ echo >> output.txt
mayankp@mayank:~/$ cat a.txt| tr "|" "n" | tail -18|head -9| tr "n" "|" >> output.txt
mayankp@mayank:~/$ echo >> output.txt
mayankp@mayank:~/$ cat a.txt| tr "|" "n" | tail -9 | tr "n" "|" >> output.txt
mayankp@mayank:~/$ echo >> output.txt
mayankp@mayank:~/$ cat output.txt
a|b|c|d|e|f|g|h|i|
j|k|l|m|n|o|p|q|r|
s|t|u|v|w|x|y|z||
@Thanks Mayank for your response but I am looking something Dynamic. If the new words are appended then it work.
– Josh
Nov 9 at 17:57
add a comment |
up vote
1
down vote
I pure ksh
version:
IFS='|'
typeset -a str=( $( < a.txt ) )
len=$#str[@]
bar='|'
for ((ii=0; ii<len; ii+=9))
do
max=9
(( ii+9 > len )) && max=$(( len-ii ))
for ((jj=0; jj<max; jj++))
do
element=$(( ii+jj ))
(( element == len-1 )) && bar=''
echo -e "$str[$element]$barc"
done
echo
done
Explanation:
IFS
: This is the Internal field separator, normally this is space and tab but if it is reassigned, it separates other types of data.typeset -a
We are creating an array to hold the data.str=( $( < a.txt ) )
We read in the data from the a.txt array as elements of the array.len=$#str[@]
We need to know how long the array is so we know how much to print out.- Outer for loop, you wanted to see the values in groups of 9. This steps through the array nine at a time.
- To avoid running off the end, I created a variable max that will hold how many elements are on the current line. The we guess 9 and then updated it if necessary.
(( ii+9 > len )) && max=$(( len-ii ))
This is doing an integer comparison of ii+9 against the length. If the condition is true, we update max with the lower value.- Inner for loop, this where we step through the elements of the line.
element=...
We want the ii+jj element so here we calculate the value.(( element == len-1 )) && bar=''
You had no bar at the end. To avoid printing the bar, we use a bar variable that we turn to nothing when done.echo -e "$str[$element]$barc"
Here we print that element along with the bar variable. The c says do not end the print with a newline. The -e is required to get the c to work.echo
Since we did not get linefeeds for each element, we need to put it out now that we have 9 elements.
This version also works with bash.
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
sed -r 's/([^|]*|)9/&n/g' a.txt
Explanation:[^|]
Not a |.[^|]*|
One field (without |
), followed by a |
.([^|]*|)9
9 fields, each followed by a |
.&n
Replace by the matched string followed by a newline.
thanks for your response. I will give it a shot
– Josh
Nov 10 at 0:50
Very nice elegant solution. Minor note, it gives a trailing bar (|
) that was not in the "expected" output but if the trailing bar is left off the input, it doesn't appear in the output.
– user1683793
Nov 10 at 1:30
add a comment |
up vote
2
down vote
accepted
sed -r 's/([^|]*|)9/&n/g' a.txt
Explanation:[^|]
Not a |.[^|]*|
One field (without |
), followed by a |
.([^|]*|)9
9 fields, each followed by a |
.&n
Replace by the matched string followed by a newline.
thanks for your response. I will give it a shot
– Josh
Nov 10 at 0:50
Very nice elegant solution. Minor note, it gives a trailing bar (|
) that was not in the "expected" output but if the trailing bar is left off the input, it doesn't appear in the output.
– user1683793
Nov 10 at 1:30
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
sed -r 's/([^|]*|)9/&n/g' a.txt
Explanation:[^|]
Not a |.[^|]*|
One field (without |
), followed by a |
.([^|]*|)9
9 fields, each followed by a |
.&n
Replace by the matched string followed by a newline.
sed -r 's/([^|]*|)9/&n/g' a.txt
Explanation:[^|]
Not a |.[^|]*|
One field (without |
), followed by a |
.([^|]*|)9
9 fields, each followed by a |
.&n
Replace by the matched string followed by a newline.
answered Nov 9 at 23:09
Walter A
10.1k2930
10.1k2930
thanks for your response. I will give it a shot
– Josh
Nov 10 at 0:50
Very nice elegant solution. Minor note, it gives a trailing bar (|
) that was not in the "expected" output but if the trailing bar is left off the input, it doesn't appear in the output.
– user1683793
Nov 10 at 1:30
add a comment |
thanks for your response. I will give it a shot
– Josh
Nov 10 at 0:50
Very nice elegant solution. Minor note, it gives a trailing bar (|
) that was not in the "expected" output but if the trailing bar is left off the input, it doesn't appear in the output.
– user1683793
Nov 10 at 1:30
thanks for your response. I will give it a shot
– Josh
Nov 10 at 0:50
thanks for your response. I will give it a shot
– Josh
Nov 10 at 0:50
Very nice elegant solution. Minor note, it gives a trailing bar (
|
) that was not in the "expected" output but if the trailing bar is left off the input, it doesn't appear in the output.– user1683793
Nov 10 at 1:30
Very nice elegant solution. Minor note, it gives a trailing bar (
|
) that was not in the "expected" output but if the trailing bar is left off the input, it doesn't appear in the output.– user1683793
Nov 10 at 1:30
add a comment |
up vote
1
down vote
Try this:
mayankp@mayank:~/$ cat a.txt| tr "|" "n" |head -9 | tr "n" "|" > output.txt
mayankp@mayank:~/$ echo >> output.txt
mayankp@mayank:~/$ cat a.txt| tr "|" "n" | tail -18|head -9| tr "n" "|" >> output.txt
mayankp@mayank:~/$ echo >> output.txt
mayankp@mayank:~/$ cat a.txt| tr "|" "n" | tail -9 | tr "n" "|" >> output.txt
mayankp@mayank:~/$ echo >> output.txt
mayankp@mayank:~/$ cat output.txt
a|b|c|d|e|f|g|h|i|
j|k|l|m|n|o|p|q|r|
s|t|u|v|w|x|y|z||
@Thanks Mayank for your response but I am looking something Dynamic. If the new words are appended then it work.
– Josh
Nov 9 at 17:57
add a comment |
up vote
1
down vote
Try this:
mayankp@mayank:~/$ cat a.txt| tr "|" "n" |head -9 | tr "n" "|" > output.txt
mayankp@mayank:~/$ echo >> output.txt
mayankp@mayank:~/$ cat a.txt| tr "|" "n" | tail -18|head -9| tr "n" "|" >> output.txt
mayankp@mayank:~/$ echo >> output.txt
mayankp@mayank:~/$ cat a.txt| tr "|" "n" | tail -9 | tr "n" "|" >> output.txt
mayankp@mayank:~/$ echo >> output.txt
mayankp@mayank:~/$ cat output.txt
a|b|c|d|e|f|g|h|i|
j|k|l|m|n|o|p|q|r|
s|t|u|v|w|x|y|z||
@Thanks Mayank for your response but I am looking something Dynamic. If the new words are appended then it work.
– Josh
Nov 9 at 17:57
add a comment |
up vote
1
down vote
up vote
1
down vote
Try this:
mayankp@mayank:~/$ cat a.txt| tr "|" "n" |head -9 | tr "n" "|" > output.txt
mayankp@mayank:~/$ echo >> output.txt
mayankp@mayank:~/$ cat a.txt| tr "|" "n" | tail -18|head -9| tr "n" "|" >> output.txt
mayankp@mayank:~/$ echo >> output.txt
mayankp@mayank:~/$ cat a.txt| tr "|" "n" | tail -9 | tr "n" "|" >> output.txt
mayankp@mayank:~/$ echo >> output.txt
mayankp@mayank:~/$ cat output.txt
a|b|c|d|e|f|g|h|i|
j|k|l|m|n|o|p|q|r|
s|t|u|v|w|x|y|z||
Try this:
mayankp@mayank:~/$ cat a.txt| tr "|" "n" |head -9 | tr "n" "|" > output.txt
mayankp@mayank:~/$ echo >> output.txt
mayankp@mayank:~/$ cat a.txt| tr "|" "n" | tail -18|head -9| tr "n" "|" >> output.txt
mayankp@mayank:~/$ echo >> output.txt
mayankp@mayank:~/$ cat a.txt| tr "|" "n" | tail -9 | tr "n" "|" >> output.txt
mayankp@mayank:~/$ echo >> output.txt
mayankp@mayank:~/$ cat output.txt
a|b|c|d|e|f|g|h|i|
j|k|l|m|n|o|p|q|r|
s|t|u|v|w|x|y|z||
answered Nov 9 at 17:55
Mayank Porwal
2,2541619
2,2541619
@Thanks Mayank for your response but I am looking something Dynamic. If the new words are appended then it work.
– Josh
Nov 9 at 17:57
add a comment |
@Thanks Mayank for your response but I am looking something Dynamic. If the new words are appended then it work.
– Josh
Nov 9 at 17:57
@Thanks Mayank for your response but I am looking something Dynamic. If the new words are appended then it work.
– Josh
Nov 9 at 17:57
@Thanks Mayank for your response but I am looking something Dynamic. If the new words are appended then it work.
– Josh
Nov 9 at 17:57
add a comment |
up vote
1
down vote
I pure ksh
version:
IFS='|'
typeset -a str=( $( < a.txt ) )
len=$#str[@]
bar='|'
for ((ii=0; ii<len; ii+=9))
do
max=9
(( ii+9 > len )) && max=$(( len-ii ))
for ((jj=0; jj<max; jj++))
do
element=$(( ii+jj ))
(( element == len-1 )) && bar=''
echo -e "$str[$element]$barc"
done
echo
done
Explanation:
IFS
: This is the Internal field separator, normally this is space and tab but if it is reassigned, it separates other types of data.typeset -a
We are creating an array to hold the data.str=( $( < a.txt ) )
We read in the data from the a.txt array as elements of the array.len=$#str[@]
We need to know how long the array is so we know how much to print out.- Outer for loop, you wanted to see the values in groups of 9. This steps through the array nine at a time.
- To avoid running off the end, I created a variable max that will hold how many elements are on the current line. The we guess 9 and then updated it if necessary.
(( ii+9 > len )) && max=$(( len-ii ))
This is doing an integer comparison of ii+9 against the length. If the condition is true, we update max with the lower value.- Inner for loop, this where we step through the elements of the line.
element=...
We want the ii+jj element so here we calculate the value.(( element == len-1 )) && bar=''
You had no bar at the end. To avoid printing the bar, we use a bar variable that we turn to nothing when done.echo -e "$str[$element]$barc"
Here we print that element along with the bar variable. The c says do not end the print with a newline. The -e is required to get the c to work.echo
Since we did not get linefeeds for each element, we need to put it out now that we have 9 elements.
This version also works with bash.
add a comment |
up vote
1
down vote
I pure ksh
version:
IFS='|'
typeset -a str=( $( < a.txt ) )
len=$#str[@]
bar='|'
for ((ii=0; ii<len; ii+=9))
do
max=9
(( ii+9 > len )) && max=$(( len-ii ))
for ((jj=0; jj<max; jj++))
do
element=$(( ii+jj ))
(( element == len-1 )) && bar=''
echo -e "$str[$element]$barc"
done
echo
done
Explanation:
IFS
: This is the Internal field separator, normally this is space and tab but if it is reassigned, it separates other types of data.typeset -a
We are creating an array to hold the data.str=( $( < a.txt ) )
We read in the data from the a.txt array as elements of the array.len=$#str[@]
We need to know how long the array is so we know how much to print out.- Outer for loop, you wanted to see the values in groups of 9. This steps through the array nine at a time.
- To avoid running off the end, I created a variable max that will hold how many elements are on the current line. The we guess 9 and then updated it if necessary.
(( ii+9 > len )) && max=$(( len-ii ))
This is doing an integer comparison of ii+9 against the length. If the condition is true, we update max with the lower value.- Inner for loop, this where we step through the elements of the line.
element=...
We want the ii+jj element so here we calculate the value.(( element == len-1 )) && bar=''
You had no bar at the end. To avoid printing the bar, we use a bar variable that we turn to nothing when done.echo -e "$str[$element]$barc"
Here we print that element along with the bar variable. The c says do not end the print with a newline. The -e is required to get the c to work.echo
Since we did not get linefeeds for each element, we need to put it out now that we have 9 elements.
This version also works with bash.
add a comment |
up vote
1
down vote
up vote
1
down vote
I pure ksh
version:
IFS='|'
typeset -a str=( $( < a.txt ) )
len=$#str[@]
bar='|'
for ((ii=0; ii<len; ii+=9))
do
max=9
(( ii+9 > len )) && max=$(( len-ii ))
for ((jj=0; jj<max; jj++))
do
element=$(( ii+jj ))
(( element == len-1 )) && bar=''
echo -e "$str[$element]$barc"
done
echo
done
Explanation:
IFS
: This is the Internal field separator, normally this is space and tab but if it is reassigned, it separates other types of data.typeset -a
We are creating an array to hold the data.str=( $( < a.txt ) )
We read in the data from the a.txt array as elements of the array.len=$#str[@]
We need to know how long the array is so we know how much to print out.- Outer for loop, you wanted to see the values in groups of 9. This steps through the array nine at a time.
- To avoid running off the end, I created a variable max that will hold how many elements are on the current line. The we guess 9 and then updated it if necessary.
(( ii+9 > len )) && max=$(( len-ii ))
This is doing an integer comparison of ii+9 against the length. If the condition is true, we update max with the lower value.- Inner for loop, this where we step through the elements of the line.
element=...
We want the ii+jj element so here we calculate the value.(( element == len-1 )) && bar=''
You had no bar at the end. To avoid printing the bar, we use a bar variable that we turn to nothing when done.echo -e "$str[$element]$barc"
Here we print that element along with the bar variable. The c says do not end the print with a newline. The -e is required to get the c to work.echo
Since we did not get linefeeds for each element, we need to put it out now that we have 9 elements.
This version also works with bash.
I pure ksh
version:
IFS='|'
typeset -a str=( $( < a.txt ) )
len=$#str[@]
bar='|'
for ((ii=0; ii<len; ii+=9))
do
max=9
(( ii+9 > len )) && max=$(( len-ii ))
for ((jj=0; jj<max; jj++))
do
element=$(( ii+jj ))
(( element == len-1 )) && bar=''
echo -e "$str[$element]$barc"
done
echo
done
Explanation:
IFS
: This is the Internal field separator, normally this is space and tab but if it is reassigned, it separates other types of data.typeset -a
We are creating an array to hold the data.str=( $( < a.txt ) )
We read in the data from the a.txt array as elements of the array.len=$#str[@]
We need to know how long the array is so we know how much to print out.- Outer for loop, you wanted to see the values in groups of 9. This steps through the array nine at a time.
- To avoid running off the end, I created a variable max that will hold how many elements are on the current line. The we guess 9 and then updated it if necessary.
(( ii+9 > len )) && max=$(( len-ii ))
This is doing an integer comparison of ii+9 against the length. If the condition is true, we update max with the lower value.- Inner for loop, this where we step through the elements of the line.
element=...
We want the ii+jj element so here we calculate the value.(( element == len-1 )) && bar=''
You had no bar at the end. To avoid printing the bar, we use a bar variable that we turn to nothing when done.echo -e "$str[$element]$barc"
Here we print that element along with the bar variable. The c says do not end the print with a newline. The -e is required to get the c to work.echo
Since we did not get linefeeds for each element, we need to put it out now that we have 9 elements.
This version also works with bash.
edited Nov 10 at 1:50
answered Nov 10 at 1:05
user1683793
658512
658512
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53230025%2fretrieve-field-rows-from-a-pipe-delimited-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What was the reasoning behind using
print NF-1
? How does it correlate with your expected output?– hek2mgl
Nov 9 at 17:40