0

I want to pass the result of a sed command to a variable to only read part of each line of a file. For example, here is the content of my input file named "fic1.txt":

-->cat fic1.txt:

/data/test/AAAA1.txt: text/plain; charset=us-ascii
/data/test/AAAA2.txt: text/plain; charset=us-ascii
/data/test/AAAA3.txt: text/plain; charset=us-ascii
/data/test/BBBB1.txt: text/plain; charset=iso-8859-1
/data/test/BBBB2.txt: text/plain; charset=iso-8859-1
/data/test/BBBB3.txt: text/plain; charset=iso-8859-1
/data/test/BBBB4.txt: text/plain; charset=iso-8859-1
/data/test/CCCC1.txt: text/plain; charset=iso-8859-1
/data/test/CCCC2.txt: text/plain; charset=unknown-8bit

So I want to use sed to pass the result on variable, such as:

-> Output_result1:

us-ascii
us-ascii
us-ascii
iso-8859-1
iso-8859-1
iso-8859-1
iso-8859-1
iso-8859-1
unknown-8bit

-> Output_result2:

/data/test/AAAA1.txt
/data/test/AAAA2.txt
/data/test/AAAA3.txt
/data/test/BBBB1.txt
/data/test/BBBB2.txt
/data/test/BBBB3.txt
/data/test/BBBB4.txt
/data/test/CCCC1.txt
/data/test/CCCC2.txt

For the first one output, I'm used this following sed command which works:

var_type_fic=`cat fic1.txt |sed -r 's/.*charset=([^ ]+).*/\1/'|sort`

for fic in $var_type_fic; do
   echo "$fic"
done

But for the second, I can't find the regex match that is going well. Can someone help please?

2
  • Welcome, use the modern $(...) for command substitution instead of backticks. Jan 14, 2022 at 17:52
  • You can use printf "%s\n" "${var_type_fic[@]}" instead of a loop. Jan 14, 2022 at 18:07

2 Answers 2

1

You're using the wrong tool. Read man cut,

cut -d= -f2 fic1.txt
cut '-d:'  -f1 fic1.txt

It's easy.

0

This is easiest done with cut as waltinator shows in their answer, but since you ask about a sed solution, here's one of those:

#n
# The above turns off the default output, just like using -n on the
# command line would do (#n must be the first two characters of the
# script).

# Save the original line to the hold space.
h

# Remove all up to and including the last equal sign.
# Write the modified text to "Output_result1".
s/.*=//
w Output_result1

# Fetch the original line from the hold space.
g

# Remove all from and including the first colon.
# Write the modified text to "Output_result2".
s/:.*//
w Output_result2

Testing it:

$ sed -f script file
$ cat Output_result1
us-ascii
us-ascii
us-ascii
iso-8859-1
iso-8859-1
iso-8859-1
iso-8859-1
iso-8859-1
unknown-8bit
$ cat Output_result2
/data/test/AAAA1.txt
/data/test/AAAA2.txt
/data/test/AAAA3.txt
/data/test/BBBB1.txt
/data/test/BBBB2.txt
/data/test/BBBB3.txt
/data/test/BBBB4.txt
/data/test/CCCC1.txt
/data/test/CCCC2.txt

Without using a separate script:

sed -n  -e h -e 's/.*=//' -e 'w Output_result1' \
        -e g -e 's/:.*//' -e 'w Output_result2' file

Depending on how you're actually planning to use this data, you may possibly want to read the data into shell variables directly. If you use : and = as delimiters, you can do that in a simple loop:

#!/bin/sh

while IFS=':=' read -r pathname junk charset
do
        printf 'pathname="%s"\tcharset="%s"\n' "$pathname" "$charset"
done <file

For the given data, this would output

pathname="/data/test/AAAA1.txt" charset="us-ascii"
pathname="/data/test/AAAA2.txt" charset="us-ascii"
pathname="/data/test/AAAA3.txt" charset="us-ascii"
pathname="/data/test/BBBB1.txt" charset="iso-8859-1"
pathname="/data/test/BBBB2.txt" charset="iso-8859-1"
pathname="/data/test/BBBB3.txt" charset="iso-8859-1"
pathname="/data/test/BBBB4.txt" charset="iso-8859-1"
pathname="/data/test/CCCC1.txt" charset="iso-8859-1"
pathname="/data/test/CCCC2.txt" charset="unknown-8bit"
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .