Add a pattern before the delimiter

Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I'm looking for a command to add a pattern before or the delimiter.
My sting:
1 |Chris|ubuntu
Here my delimiter is| and I want to generate a string with this input.
Output
1 = ID and Chris = Name and Ubuntu = OS
Sometimes, my input has 2 values only.
Let's say 2 | Ram, which has one value and no delimiter, so it should print:
2=ID and Ram=Name
So, field1 + "and" + field2 + "and" + field3. If field3 is not available in the input string then use field1 + "and" + field2.
linux shell-script text-processing csv
add a comment |
up vote
1
down vote
favorite
I'm looking for a command to add a pattern before or the delimiter.
My sting:
1 |Chris|ubuntu
Here my delimiter is| and I want to generate a string with this input.
Output
1 = ID and Chris = Name and Ubuntu = OS
Sometimes, my input has 2 values only.
Let's say 2 | Ram, which has one value and no delimiter, so it should print:
2=ID and Ram=Name
So, field1 + "and" + field2 + "and" + field3. If field3 is not available in the input string then use field1 + "and" + field2.
linux shell-script text-processing csv
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm looking for a command to add a pattern before or the delimiter.
My sting:
1 |Chris|ubuntu
Here my delimiter is| and I want to generate a string with this input.
Output
1 = ID and Chris = Name and Ubuntu = OS
Sometimes, my input has 2 values only.
Let's say 2 | Ram, which has one value and no delimiter, so it should print:
2=ID and Ram=Name
So, field1 + "and" + field2 + "and" + field3. If field3 is not available in the input string then use field1 + "and" + field2.
linux shell-script text-processing csv
I'm looking for a command to add a pattern before or the delimiter.
My sting:
1 |Chris|ubuntu
Here my delimiter is| and I want to generate a string with this input.
Output
1 = ID and Chris = Name and Ubuntu = OS
Sometimes, my input has 2 values only.
Let's say 2 | Ram, which has one value and no delimiter, so it should print:
2=ID and Ram=Name
So, field1 + "and" + field2 + "and" + field3. If field3 is not available in the input string then use field1 + "and" + field2.
linux shell-script text-processing csv
linux shell-script text-processing csv
edited Nov 26 at 20:40
Jeff Schaller
37k1052121
37k1052121
asked Nov 26 at 19:34
user322904
61
61
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
awk -F '|' '
printf("%d = ID and %s = Name", $1, $2)
NF == 3 printf(" and %s = OS", $3)
printf("n") ' file
This would generate
1 = ID and Chris = Name and ubuntu = OS
2 = ID and Ram = Name
for the given data. The awk code simply inserts the two first fields into the printf format template. If a third field is available, the OS part is then outputted on the same line. The line is then terminated by a newline.
add a comment |
up vote
0
down vote
In Bash:
#!/usr/bin/env bash
if [ "$#" -eq 0 ]
then
printf "Missing argumentn" >&2
exit 1
fi
num_of_delimiters="$(grep -o '|' <<< "$1" | wc -l)"
sting="$(sed -E 's,s+|,|,g' <<< "$1")"
sting="$(sed -E 's,|s+,|,g' <<< "$sting")"
case "$num_of_delimiters" in
1)
echo "$(cut -d '|' -f1 <<< "$sting")"=ID and "$(cut -d '|' -f2 <<< "$sting")"=Name
;;
2)
echo "$(cut -d '|' -f1 <<< "$sting")" = ID and "$(cut -d '|' -f2 <<< "$sting")" = Name and
"$(sed 's/./U&/' <<< "$(cut -d '|' -f3 <<< "$sting")")" = OS
;;
*)
printf "More than 2 delimiters or no delimitersn" >&2
exit 2
esac
It will also print whitespaces as you showed in your example and will
convert the first letter of the third word to uppercase. Examples:
$ ./sting.sh "2 | Ram"
2=ID and Ram=Name
$ ./sting.sh "1 |Chris|ubuntu"
1 = ID and Chris = Name and Ubuntu = OS
No errors reported by https://www.shellcheck.net/.
Hey, Im also doing a similar kind of process, But for me the inputs may contains more than 2 delimiters. In this case, how can I remove that restriction?
– SQLadmin
Nov 26 at 20:48
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
awk -F '|' '
printf("%d = ID and %s = Name", $1, $2)
NF == 3 printf(" and %s = OS", $3)
printf("n") ' file
This would generate
1 = ID and Chris = Name and ubuntu = OS
2 = ID and Ram = Name
for the given data. The awk code simply inserts the two first fields into the printf format template. If a third field is available, the OS part is then outputted on the same line. The line is then terminated by a newline.
add a comment |
up vote
1
down vote
awk -F '|' '
printf("%d = ID and %s = Name", $1, $2)
NF == 3 printf(" and %s = OS", $3)
printf("n") ' file
This would generate
1 = ID and Chris = Name and ubuntu = OS
2 = ID and Ram = Name
for the given data. The awk code simply inserts the two first fields into the printf format template. If a third field is available, the OS part is then outputted on the same line. The line is then terminated by a newline.
add a comment |
up vote
1
down vote
up vote
1
down vote
awk -F '|' '
printf("%d = ID and %s = Name", $1, $2)
NF == 3 printf(" and %s = OS", $3)
printf("n") ' file
This would generate
1 = ID and Chris = Name and ubuntu = OS
2 = ID and Ram = Name
for the given data. The awk code simply inserts the two first fields into the printf format template. If a third field is available, the OS part is then outputted on the same line. The line is then terminated by a newline.
awk -F '|' '
printf("%d = ID and %s = Name", $1, $2)
NF == 3 printf(" and %s = OS", $3)
printf("n") ' file
This would generate
1 = ID and Chris = Name and ubuntu = OS
2 = ID and Ram = Name
for the given data. The awk code simply inserts the two first fields into the printf format template. If a third field is available, the OS part is then outputted on the same line. The line is then terminated by a newline.
answered Nov 26 at 20:47
Kusalananda
118k16223364
118k16223364
add a comment |
add a comment |
up vote
0
down vote
In Bash:
#!/usr/bin/env bash
if [ "$#" -eq 0 ]
then
printf "Missing argumentn" >&2
exit 1
fi
num_of_delimiters="$(grep -o '|' <<< "$1" | wc -l)"
sting="$(sed -E 's,s+|,|,g' <<< "$1")"
sting="$(sed -E 's,|s+,|,g' <<< "$sting")"
case "$num_of_delimiters" in
1)
echo "$(cut -d '|' -f1 <<< "$sting")"=ID and "$(cut -d '|' -f2 <<< "$sting")"=Name
;;
2)
echo "$(cut -d '|' -f1 <<< "$sting")" = ID and "$(cut -d '|' -f2 <<< "$sting")" = Name and
"$(sed 's/./U&/' <<< "$(cut -d '|' -f3 <<< "$sting")")" = OS
;;
*)
printf "More than 2 delimiters or no delimitersn" >&2
exit 2
esac
It will also print whitespaces as you showed in your example and will
convert the first letter of the third word to uppercase. Examples:
$ ./sting.sh "2 | Ram"
2=ID and Ram=Name
$ ./sting.sh "1 |Chris|ubuntu"
1 = ID and Chris = Name and Ubuntu = OS
No errors reported by https://www.shellcheck.net/.
Hey, Im also doing a similar kind of process, But for me the inputs may contains more than 2 delimiters. In this case, how can I remove that restriction?
– SQLadmin
Nov 26 at 20:48
add a comment |
up vote
0
down vote
In Bash:
#!/usr/bin/env bash
if [ "$#" -eq 0 ]
then
printf "Missing argumentn" >&2
exit 1
fi
num_of_delimiters="$(grep -o '|' <<< "$1" | wc -l)"
sting="$(sed -E 's,s+|,|,g' <<< "$1")"
sting="$(sed -E 's,|s+,|,g' <<< "$sting")"
case "$num_of_delimiters" in
1)
echo "$(cut -d '|' -f1 <<< "$sting")"=ID and "$(cut -d '|' -f2 <<< "$sting")"=Name
;;
2)
echo "$(cut -d '|' -f1 <<< "$sting")" = ID and "$(cut -d '|' -f2 <<< "$sting")" = Name and
"$(sed 's/./U&/' <<< "$(cut -d '|' -f3 <<< "$sting")")" = OS
;;
*)
printf "More than 2 delimiters or no delimitersn" >&2
exit 2
esac
It will also print whitespaces as you showed in your example and will
convert the first letter of the third word to uppercase. Examples:
$ ./sting.sh "2 | Ram"
2=ID and Ram=Name
$ ./sting.sh "1 |Chris|ubuntu"
1 = ID and Chris = Name and Ubuntu = OS
No errors reported by https://www.shellcheck.net/.
Hey, Im also doing a similar kind of process, But for me the inputs may contains more than 2 delimiters. In this case, how can I remove that restriction?
– SQLadmin
Nov 26 at 20:48
add a comment |
up vote
0
down vote
up vote
0
down vote
In Bash:
#!/usr/bin/env bash
if [ "$#" -eq 0 ]
then
printf "Missing argumentn" >&2
exit 1
fi
num_of_delimiters="$(grep -o '|' <<< "$1" | wc -l)"
sting="$(sed -E 's,s+|,|,g' <<< "$1")"
sting="$(sed -E 's,|s+,|,g' <<< "$sting")"
case "$num_of_delimiters" in
1)
echo "$(cut -d '|' -f1 <<< "$sting")"=ID and "$(cut -d '|' -f2 <<< "$sting")"=Name
;;
2)
echo "$(cut -d '|' -f1 <<< "$sting")" = ID and "$(cut -d '|' -f2 <<< "$sting")" = Name and
"$(sed 's/./U&/' <<< "$(cut -d '|' -f3 <<< "$sting")")" = OS
;;
*)
printf "More than 2 delimiters or no delimitersn" >&2
exit 2
esac
It will also print whitespaces as you showed in your example and will
convert the first letter of the third word to uppercase. Examples:
$ ./sting.sh "2 | Ram"
2=ID and Ram=Name
$ ./sting.sh "1 |Chris|ubuntu"
1 = ID and Chris = Name and Ubuntu = OS
No errors reported by https://www.shellcheck.net/.
In Bash:
#!/usr/bin/env bash
if [ "$#" -eq 0 ]
then
printf "Missing argumentn" >&2
exit 1
fi
num_of_delimiters="$(grep -o '|' <<< "$1" | wc -l)"
sting="$(sed -E 's,s+|,|,g' <<< "$1")"
sting="$(sed -E 's,|s+,|,g' <<< "$sting")"
case "$num_of_delimiters" in
1)
echo "$(cut -d '|' -f1 <<< "$sting")"=ID and "$(cut -d '|' -f2 <<< "$sting")"=Name
;;
2)
echo "$(cut -d '|' -f1 <<< "$sting")" = ID and "$(cut -d '|' -f2 <<< "$sting")" = Name and
"$(sed 's/./U&/' <<< "$(cut -d '|' -f3 <<< "$sting")")" = OS
;;
*)
printf "More than 2 delimiters or no delimitersn" >&2
exit 2
esac
It will also print whitespaces as you showed in your example and will
convert the first letter of the third word to uppercase. Examples:
$ ./sting.sh "2 | Ram"
2=ID and Ram=Name
$ ./sting.sh "1 |Chris|ubuntu"
1 = ID and Chris = Name and Ubuntu = OS
No errors reported by https://www.shellcheck.net/.
answered Nov 26 at 20:30
Arkadiusz Drabczyk
7,68021734
7,68021734
Hey, Im also doing a similar kind of process, But for me the inputs may contains more than 2 delimiters. In this case, how can I remove that restriction?
– SQLadmin
Nov 26 at 20:48
add a comment |
Hey, Im also doing a similar kind of process, But for me the inputs may contains more than 2 delimiters. In this case, how can I remove that restriction?
– SQLadmin
Nov 26 at 20:48
Hey, Im also doing a similar kind of process, But for me the inputs may contains more than 2 delimiters. In this case, how can I remove that restriction?
– SQLadmin
Nov 26 at 20:48
Hey, Im also doing a similar kind of process, But for me the inputs may contains more than 2 delimiters. In this case, how can I remove that restriction?
– SQLadmin
Nov 26 at 20:48
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2funix.stackexchange.com%2fquestions%2f484274%2fadd-a-pattern-before-the-delimiter%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