Skip to main content
added 183 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

The issue with your code is that you try to run a sed command that ooks like

2 5 7 10p

This obviously makes no sense to sed, so it complains.

Part of your issue is using a single string to hold many different items (the line numbers). This is better done with an array. Each array element (line number) will then need to have a p inserted after it to make it a separate sed command.

For the numbers 2, 5, 7, and 10, you want a sed script that looks something like

2p; 5p; 7p; 10p;

or

2p;5p;7p;10p;

or

2p
5p
7p
10p

You can create a script like that rather easily from an array in bash:

lines=( 2 5 7 10 )
printf '%sp\n' "${lines[@]:-"q;"}" | sed -n -f /dev/stdin longText.txt

This uses printf to create a sed script from the list of numbers, and feeds it to sed which reads it from standard input with -f /dev/stdin, and applies it to you file.

If the list is empty, the printf will emit a sed expression that does not do anything and then terminates sed.


If you need to use the numbers in a string, then you could convert that string into a valid sed script and apply it to your file like so:

sed 's/[[:digit:]]\{1,\}/&p;/g' <<<"$list" | sed -n -f /dev/stdin longText.txt

Given the string 2 5 7 10, this would generate the sed script 2p; 5p; 7p; 10p; and then apply that to your file in a similar manner as above.

An empty list would not cause sed to output anything.

The issue with your code is that you try to run a sed command that ooks like

2 5 7 10p

This obviously makes no sense to sed, so it complains.

Part of your issue is using a single string to hold many different items (the line numbers). This is better done with an array. Each array element (line number) will then need to have a p inserted after it to make it a separate sed command.

For the numbers 2, 5, 7, and 10, you want a sed script that looks something like

2p; 5p; 7p; 10p;

or

2p;5p;7p;10p;

or

2p
5p
7p
10p

You can create a script like that rather easily from an array in bash:

lines=( 2 5 7 10 )
printf '%sp\n' "${lines[@]}" | sed -n -f /dev/stdin longText.txt

This uses printf to create a sed script from the list of numbers, and feeds it to sed which reads it from standard input with -f /dev/stdin, and applies it to you file.


If you need to use the numbers in a string, then you could convert that string into a valid sed script and apply it to your file like so:

sed 's/[[:digit:]]\{1,\}/&p;/g' <<<"$list" | sed -n -f /dev/stdin longText.txt

Given the string 2 5 7 10, this would generate the sed script 2p; 5p; 7p; 10p; and then apply that to your file in a similar manner as above.

The issue with your code is that you try to run a sed command that ooks like

2 5 7 10p

This obviously makes no sense to sed, so it complains.

Part of your issue is using a single string to hold many different items (the line numbers). This is better done with an array. Each array element (line number) will then need to have a p inserted after it to make it a separate sed command.

For the numbers 2, 5, 7, and 10, you want a sed script that looks something like

2p; 5p; 7p; 10p;

or

2p;5p;7p;10p;

or

2p
5p
7p
10p

You can create a script like that rather easily from an array in bash:

lines=( 2 5 7 10 )
printf '%sp\n' "${lines[@]:-"q;"}" | sed -n -f /dev/stdin longText.txt

This uses printf to create a sed script from the list of numbers, and feeds it to sed which reads it from standard input with -f /dev/stdin, and applies it to you file.

If the list is empty, the printf will emit a sed expression that does not do anything and then terminates sed.


If you need to use the numbers in a string, then you could convert that string into a valid sed script and apply it to your file like so:

sed 's/[[:digit:]]\{1,\}/&p;/g' <<<"$list" | sed -n -f /dev/stdin longText.txt

Given the string 2 5 7 10, this would generate the sed script 2p; 5p; 7p; 10p; and then apply that to your file in a similar manner as above.

An empty list would not cause sed to output anything.

deleted 53 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

The issue with your code is that you try to run a sed command that ooks like

2 5 7 10p

This obviously makes no sense to sed, so it complains.

Part of your issue is using a single string to hold many different items (the line numbers). This is better done with an array. Each array element (line number) will then need to have a p inserted after it to make it a separate sed command.

For the numbers 2, 5, 7, and 10, you want a sed script that looks something like

2p; 5p; 7p; 10p;

or

2p;5p;7p;10p;

or

2p
5p
7p
10p

You can create a script like that rather easily from an array in bash:

lines=( 2 5 7 10 )
printf '%sp\n' "${lines[@]}" | sed -n -f /dev/stdin longText.txt

This uses printf to create a sed script from the list of numbers, and feeds it to sed which reads it from standard input with -f /dev/stdin, and applies it to you file.


If you need to use the numbers in a string, then you could convert that string into a valid sed script and apply it to your file like so:

sed 's/[[:digit:]]\{1,\}/&p;/g' <<<"$list" | sed -n -f /dev/stdin longText.txt

Given the string 2 5 7 10, this would generate the sed script 2p; 5p; 7p; 10p; and then apply that to your file in a similar manner as above.

The issue with your code is that you try to run a sed command that ooks like

2 5 7 10p

This obviously makes no sense to sed, so it complains.

Part of your issue is using a single string to hold many different items (the line numbers). This is better done with an array. Each array element (line number) will then need to have a p inserted after it to make it a separate sed command.

For the numbers 2, 5, 7, and 10, you want a sed script that looks something like

2p; 5p; 7p; 10p;

or

2p;5p;7p;10p;

or

2p
5p
7p
10p

You can create a script like that rather easily from an array in bash:

lines=( 2 5 7 10 )
printf '%sp\n' "${lines[@]}" | sed -n -f /dev/stdin longText.txt

This uses printf to create a sed script from the list of numbers, and feeds it to sed which reads it from standard input with -f /dev/stdin, and applies it to you file.

The issue with your code is that you try to run a sed command that ooks like

2 5 7 10p

This obviously makes no sense to sed, so it complains.

Part of your issue is using a single string to hold many different items (the line numbers). This is better done with an array. Each array element (line number) will then need to have a p inserted after it to make it a separate sed command.

For the numbers 2, 5, 7, and 10, you want a sed script that looks something like

2p; 5p; 7p; 10p;

or

2p;5p;7p;10p;

or

2p
5p
7p
10p

You can create a script like that rather easily from an array in bash:

lines=( 2 5 7 10 )
printf '%sp\n' "${lines[@]}" | sed -n -f /dev/stdin longText.txt

This uses printf to create a sed script from the list of numbers, and feeds it to sed which reads it from standard input with -f /dev/stdin, and applies it to you file.


If you need to use the numbers in a string, then you could convert that string into a valid sed script and apply it to your file like so:

sed 's/[[:digit:]]\{1,\}/&p;/g' <<<"$list" | sed -n -f /dev/stdin longText.txt

Given the string 2 5 7 10, this would generate the sed script 2p; 5p; 7p; 10p; and then apply that to your file in a similar manner as above.

deleted 53 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

The issue with your code is that you try to run a sed command that ooks like

2 5 7 10p

This obviously makes no sense to sed, so it complains.

Part of your issue is using a single string to hold many different items (the line numbers). This is better done with an array. Each array element (line number) will then need to have a p inserted after it to make it a separate sed command.

For the numbers 2, 5, 7, and 10, you want a sed script that looks something like

2p; 5p; 7p; 10p;

(where the spaces are optional and where each ; could be replaced by a literal newline, which is what we do below). or

2p;5p;7p;10p;

or

2p
5p
7p
10p

You can create a script like that rather easily from an array in bash:

lines=( 2 5 7 10 )
printf '%sp\n' "${lines[@]}" | sed -n -f /dev/stdin longText.txt

This uses printf to create a sed script from the list of numbers, and feeds it to sed which reads it from standard input with -f /dev/stdin, and applies it to you file.

The issue with your code is that you try to run a sed command that ooks like

2 5 7 10p

This obviously makes no sense to sed, so it complains.

Part of your issue is using a single string to hold many different items (the line numbers). This is better done with an array. Each array element (line number) will then need to have a p inserted after it to make it a separate sed command.

For the numbers 2, 5, 7, and 10, you want a sed script that looks something like

2p; 5p; 7p; 10p;

(where the spaces are optional and where each ; could be replaced by a literal newline, which is what we do below).

You can create a script like that rather easily from an array in bash:

lines=( 2 5 7 10 )
printf '%sp\n' "${lines[@]}" | sed -n -f /dev/stdin longText.txt

This uses printf to create a sed script from the list of numbers, and feeds it to sed which reads it from standard input with -f /dev/stdin, and applies it to you file.

The issue with your code is that you try to run a sed command that ooks like

2 5 7 10p

This obviously makes no sense to sed, so it complains.

Part of your issue is using a single string to hold many different items (the line numbers). This is better done with an array. Each array element (line number) will then need to have a p inserted after it to make it a separate sed command.

For the numbers 2, 5, 7, and 10, you want a sed script that looks something like

2p; 5p; 7p; 10p;

or

2p;5p;7p;10p;

or

2p
5p
7p
10p

You can create a script like that rather easily from an array in bash:

lines=( 2 5 7 10 )
printf '%sp\n' "${lines[@]}" | sed -n -f /dev/stdin longText.txt

This uses printf to create a sed script from the list of numbers, and feeds it to sed which reads it from standard input with -f /dev/stdin, and applies it to you file.

Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
Loading