Read from a file and return new line with loop
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
I have a file like
foo.txt
xxx B=C D: A
yyy F=H D:A
zzz K=L D: A
fff M=H D:/llll
kkk S=D D: /kkkkk
this is what I try to get;
xxx B=C; D: A ;pass
yyy F=H; D:A ;pass
zzz K=L; D: A ;pass
fff M=H; D:/llll ;try
kkk S=D; D: /kkkkk/bb ;try
The second part of line which starts with "D:",if starts with "/", it should be write "try" else it has to write "pass" end of the line for every line in it.
I can read file like this but there is spaces which is another problem
In addition after "D:" always starts with "A" or "/".
I tried while loop but I have not succeeded.
cat /tmp/foo.txt | cut -d ":" -f,2
Output
A (one empty space)
A (none)
A (three empty space)
/llll (none)
/kkkkk/bb (two empty space)
This is a example what I have tried
while read -r line;
do
if [[ $line ]]; then
echo $line "try"
else
echo $line "pass"
fi
done < foo.txt
bash shell-script
add a comment |
up vote
2
down vote
favorite
I have a file like
foo.txt
xxx B=C D: A
yyy F=H D:A
zzz K=L D: A
fff M=H D:/llll
kkk S=D D: /kkkkk
this is what I try to get;
xxx B=C; D: A ;pass
yyy F=H; D:A ;pass
zzz K=L; D: A ;pass
fff M=H; D:/llll ;try
kkk S=D; D: /kkkkk/bb ;try
The second part of line which starts with "D:",if starts with "/", it should be write "try" else it has to write "pass" end of the line for every line in it.
I can read file like this but there is spaces which is another problem
In addition after "D:" always starts with "A" or "/".
I tried while loop but I have not succeeded.
cat /tmp/foo.txt | cut -d ":" -f,2
Output
A (one empty space)
A (none)
A (three empty space)
/llll (none)
/kkkkk/bb (two empty space)
This is a example what I have tried
while read -r line;
do
if [[ $line ]]; then
echo $line "try"
else
echo $line "pass"
fi
done < foo.txt
bash shell-script
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have a file like
foo.txt
xxx B=C D: A
yyy F=H D:A
zzz K=L D: A
fff M=H D:/llll
kkk S=D D: /kkkkk
this is what I try to get;
xxx B=C; D: A ;pass
yyy F=H; D:A ;pass
zzz K=L; D: A ;pass
fff M=H; D:/llll ;try
kkk S=D; D: /kkkkk/bb ;try
The second part of line which starts with "D:",if starts with "/", it should be write "try" else it has to write "pass" end of the line for every line in it.
I can read file like this but there is spaces which is another problem
In addition after "D:" always starts with "A" or "/".
I tried while loop but I have not succeeded.
cat /tmp/foo.txt | cut -d ":" -f,2
Output
A (one empty space)
A (none)
A (three empty space)
/llll (none)
/kkkkk/bb (two empty space)
This is a example what I have tried
while read -r line;
do
if [[ $line ]]; then
echo $line "try"
else
echo $line "pass"
fi
done < foo.txt
bash shell-script
I have a file like
foo.txt
xxx B=C D: A
yyy F=H D:A
zzz K=L D: A
fff M=H D:/llll
kkk S=D D: /kkkkk
this is what I try to get;
xxx B=C; D: A ;pass
yyy F=H; D:A ;pass
zzz K=L; D: A ;pass
fff M=H; D:/llll ;try
kkk S=D; D: /kkkkk/bb ;try
The second part of line which starts with "D:",if starts with "/", it should be write "try" else it has to write "pass" end of the line for every line in it.
I can read file like this but there is spaces which is another problem
In addition after "D:" always starts with "A" or "/".
I tried while loop but I have not succeeded.
cat /tmp/foo.txt | cut -d ":" -f,2
Output
A (one empty space)
A (none)
A (three empty space)
/llll (none)
/kkkkk/bb (two empty space)
This is a example what I have tried
while read -r line;
do
if [[ $line ]]; then
echo $line "try"
else
echo $line "pass"
fi
done < foo.txt
bash shell-script
bash shell-script
edited yesterday
Marco Bonelli
23312
23312
asked yesterday
1010111100011
356
356
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
0
down vote
accepted
You can't write in the same file, I would recommend to create a new file and then rename it.
tmpFile="/tmp/foo2.txt.tmp"
cat /tmp/foo.txt | while read line; do
isSlash=$(echo $line | cut -d ":" -f2 | awk 'print $1' | grep -c ^"/")
if (( $isSlash )); then
echo "$line ;try" >> $tmpFile
else
echo "$line ;pass" >> $tmpFile
fi
done
mv $tmpFile /tmp/foo.txt
kkk S=D D: /kkkkk ;pass it writes pass beacause of the empty space before the "/" so can we add something that ignore the empty spaces?
– 1010111100011
yesterday
I updated my answer,awk 'print $1'
will solve the issue with the empty space.
– Javier Salas
yesterday
thank you @JavierSalas
– 1010111100011
yesterday
add a comment |
up vote
1
down vote
You can use awk
, which makes it very easy to accomplish what you want:
awk '!/D: *//print $0 " ;pass"; next print $0 " ;try"' foo.txt
Output:
xxx B=C D: A ;pass
yyy F=H D:A ;pass
zzz K=L D: A ;pass
fff M=H D:/llll ;try
kkk S=D D: /kkkkk ;try
This basically matches every line not containing D:
and /
and appends ;pass
to it, then skips to the next line. Otherwise it appends ;try
to it.
add a comment |
up vote
0
down vote
sed
OK?
$ sed '/D: *A/ s/$/t;pass/; /D: *// s/$/t;try/' file
xxx B=C D: A ;pass
yyy F=H D:A ;pass
zzz K=L D: A ;pass
fff M=H D:/llll ;try
kkk S=D D: /kkkkk ;try
posixly:sed -e 's+D: */.*+&t;try+ ; t' -e 's+$+t;pass+'
– Isaac
yesterday
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
You can't write in the same file, I would recommend to create a new file and then rename it.
tmpFile="/tmp/foo2.txt.tmp"
cat /tmp/foo.txt | while read line; do
isSlash=$(echo $line | cut -d ":" -f2 | awk 'print $1' | grep -c ^"/")
if (( $isSlash )); then
echo "$line ;try" >> $tmpFile
else
echo "$line ;pass" >> $tmpFile
fi
done
mv $tmpFile /tmp/foo.txt
kkk S=D D: /kkkkk ;pass it writes pass beacause of the empty space before the "/" so can we add something that ignore the empty spaces?
– 1010111100011
yesterday
I updated my answer,awk 'print $1'
will solve the issue with the empty space.
– Javier Salas
yesterday
thank you @JavierSalas
– 1010111100011
yesterday
add a comment |
up vote
0
down vote
accepted
You can't write in the same file, I would recommend to create a new file and then rename it.
tmpFile="/tmp/foo2.txt.tmp"
cat /tmp/foo.txt | while read line; do
isSlash=$(echo $line | cut -d ":" -f2 | awk 'print $1' | grep -c ^"/")
if (( $isSlash )); then
echo "$line ;try" >> $tmpFile
else
echo "$line ;pass" >> $tmpFile
fi
done
mv $tmpFile /tmp/foo.txt
kkk S=D D: /kkkkk ;pass it writes pass beacause of the empty space before the "/" so can we add something that ignore the empty spaces?
– 1010111100011
yesterday
I updated my answer,awk 'print $1'
will solve the issue with the empty space.
– Javier Salas
yesterday
thank you @JavierSalas
– 1010111100011
yesterday
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
You can't write in the same file, I would recommend to create a new file and then rename it.
tmpFile="/tmp/foo2.txt.tmp"
cat /tmp/foo.txt | while read line; do
isSlash=$(echo $line | cut -d ":" -f2 | awk 'print $1' | grep -c ^"/")
if (( $isSlash )); then
echo "$line ;try" >> $tmpFile
else
echo "$line ;pass" >> $tmpFile
fi
done
mv $tmpFile /tmp/foo.txt
You can't write in the same file, I would recommend to create a new file and then rename it.
tmpFile="/tmp/foo2.txt.tmp"
cat /tmp/foo.txt | while read line; do
isSlash=$(echo $line | cut -d ":" -f2 | awk 'print $1' | grep -c ^"/")
if (( $isSlash )); then
echo "$line ;try" >> $tmpFile
else
echo "$line ;pass" >> $tmpFile
fi
done
mv $tmpFile /tmp/foo.txt
edited yesterday
answered yesterday
Javier Salas
1266
1266
kkk S=D D: /kkkkk ;pass it writes pass beacause of the empty space before the "/" so can we add something that ignore the empty spaces?
– 1010111100011
yesterday
I updated my answer,awk 'print $1'
will solve the issue with the empty space.
– Javier Salas
yesterday
thank you @JavierSalas
– 1010111100011
yesterday
add a comment |
kkk S=D D: /kkkkk ;pass it writes pass beacause of the empty space before the "/" so can we add something that ignore the empty spaces?
– 1010111100011
yesterday
I updated my answer,awk 'print $1'
will solve the issue with the empty space.
– Javier Salas
yesterday
thank you @JavierSalas
– 1010111100011
yesterday
kkk S=D D: /kkkkk ;pass it writes pass beacause of the empty space before the "/" so can we add something that ignore the empty spaces?
– 1010111100011
yesterday
kkk S=D D: /kkkkk ;pass it writes pass beacause of the empty space before the "/" so can we add something that ignore the empty spaces?
– 1010111100011
yesterday
I updated my answer,
awk 'print $1'
will solve the issue with the empty space.– Javier Salas
yesterday
I updated my answer,
awk 'print $1'
will solve the issue with the empty space.– Javier Salas
yesterday
thank you @JavierSalas
– 1010111100011
yesterday
thank you @JavierSalas
– 1010111100011
yesterday
add a comment |
up vote
1
down vote
You can use awk
, which makes it very easy to accomplish what you want:
awk '!/D: *//print $0 " ;pass"; next print $0 " ;try"' foo.txt
Output:
xxx B=C D: A ;pass
yyy F=H D:A ;pass
zzz K=L D: A ;pass
fff M=H D:/llll ;try
kkk S=D D: /kkkkk ;try
This basically matches every line not containing D:
and /
and appends ;pass
to it, then skips to the next line. Otherwise it appends ;try
to it.
add a comment |
up vote
1
down vote
You can use awk
, which makes it very easy to accomplish what you want:
awk '!/D: *//print $0 " ;pass"; next print $0 " ;try"' foo.txt
Output:
xxx B=C D: A ;pass
yyy F=H D:A ;pass
zzz K=L D: A ;pass
fff M=H D:/llll ;try
kkk S=D D: /kkkkk ;try
This basically matches every line not containing D:
and /
and appends ;pass
to it, then skips to the next line. Otherwise it appends ;try
to it.
add a comment |
up vote
1
down vote
up vote
1
down vote
You can use awk
, which makes it very easy to accomplish what you want:
awk '!/D: *//print $0 " ;pass"; next print $0 " ;try"' foo.txt
Output:
xxx B=C D: A ;pass
yyy F=H D:A ;pass
zzz K=L D: A ;pass
fff M=H D:/llll ;try
kkk S=D D: /kkkkk ;try
This basically matches every line not containing D:
and /
and appends ;pass
to it, then skips to the next line. Otherwise it appends ;try
to it.
You can use awk
, which makes it very easy to accomplish what you want:
awk '!/D: *//print $0 " ;pass"; next print $0 " ;try"' foo.txt
Output:
xxx B=C D: A ;pass
yyy F=H D:A ;pass
zzz K=L D: A ;pass
fff M=H D:/llll ;try
kkk S=D D: /kkkkk ;try
This basically matches every line not containing D:
and /
and appends ;pass
to it, then skips to the next line. Otherwise it appends ;try
to it.
answered yesterday
Marco Bonelli
23312
23312
add a comment |
add a comment |
up vote
0
down vote
sed
OK?
$ sed '/D: *A/ s/$/t;pass/; /D: *// s/$/t;try/' file
xxx B=C D: A ;pass
yyy F=H D:A ;pass
zzz K=L D: A ;pass
fff M=H D:/llll ;try
kkk S=D D: /kkkkk ;try
posixly:sed -e 's+D: */.*+&t;try+ ; t' -e 's+$+t;pass+'
– Isaac
yesterday
add a comment |
up vote
0
down vote
sed
OK?
$ sed '/D: *A/ s/$/t;pass/; /D: *// s/$/t;try/' file
xxx B=C D: A ;pass
yyy F=H D:A ;pass
zzz K=L D: A ;pass
fff M=H D:/llll ;try
kkk S=D D: /kkkkk ;try
posixly:sed -e 's+D: */.*+&t;try+ ; t' -e 's+$+t;pass+'
– Isaac
yesterday
add a comment |
up vote
0
down vote
up vote
0
down vote
sed
OK?
$ sed '/D: *A/ s/$/t;pass/; /D: *// s/$/t;try/' file
xxx B=C D: A ;pass
yyy F=H D:A ;pass
zzz K=L D: A ;pass
fff M=H D:/llll ;try
kkk S=D D: /kkkkk ;try
sed
OK?
$ sed '/D: *A/ s/$/t;pass/; /D: *// s/$/t;try/' file
xxx B=C D: A ;pass
yyy F=H D:A ;pass
zzz K=L D: A ;pass
fff M=H D:/llll ;try
kkk S=D D: /kkkkk ;try
answered yesterday
RudiC
2,9311211
2,9311211
posixly:sed -e 's+D: */.*+&t;try+ ; t' -e 's+$+t;pass+'
– Isaac
yesterday
add a comment |
posixly:sed -e 's+D: */.*+&t;try+ ; t' -e 's+$+t;pass+'
– Isaac
yesterday
posixly:
sed -e 's+D: */.*+&t;try+ ; t' -e 's+$+t;pass+'
– Isaac
yesterday
posixly:
sed -e 's+D: */.*+&t;try+ ; t' -e 's+$+t;pass+'
– Isaac
yesterday
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%2funix.stackexchange.com%2fquestions%2f481519%2fread-from-a-file-and-return-new-line-with-loop%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