Malik
Malik

Reputation: 195

sed while loop text format

i need to change some lines i another format using this while loop.

while IFS= read -r line; 
do 
var=$(echo "$line" | grep -oE "http://img[0-9].domain.xy/t/[0-9][0-9][0-9]/[0-9][0-9][0-9]/" | uniq);
echo "$line" | sed -e 's|http://img[0-9].domain.xy/t/[0-9][0-9][0-9]/[0-9][0-9][0-9]/||g' -e "s|.*|&"${var}"|g" >> newFile; 
done < file;

that changes this format

<iframe  src="http://domain.xy/load.php?file=2259929" frameborder="0" scrolling="no"></iframe>|http://img9.domain.xy/t/929/320/1_2259929.jpg;http://img9.domain.xy/t/929/320/2_2259929.jpg;http://img9.domain.xy/t/929/320/3_2259929.jpg;http://img9.domain.xy/t/929/320/4_2259929.jpg;http://img9.domain.xy/t/929/320/5_2259929.jpg;http://img9.domain.xy/t/929/320/6_2259929.jpg;http://img9.domain.xy/t/929/320/7_2259929.jpg;http://img9.domain.xy/t/929/320/8_2259929.jpg;http://img9.domain.xy/t/929/320/9_2259929.jpg;http://img9.domain.xy/t/929/320/10_2259929.jpg|13m5s

and gives me that output.

 <iframe  src="http://domain.xy/load.php?file=2259929" frameborder="0" scrolling="no"></iframe>|1_2259929.jpg;2_2259929.jpg;3_2259929.jpg;4_2259929.jpg;5_2259929.jpg;6_2259929.jpg;7_2259929.jpg;8_2259929.jpg;9_2259929.jpg;10_2259929.jpg|13m5s|http://img9.domain.xy/t/929/320/

that all works correct!!!

but there is also a time value that i want to change. 13m5s to 00:13:5 or better else 13m5s to 00:13:05

i try to use another grep + sed command at the end of the loop.

while IFS= read -r line; 
do 
var=$(echo "$line" | grep -oE "http://img[0-9].domain.xy/t/[0-9][0-9][0-9]/[0-9][0-9][0-9]/" | uniq);
echo "$line" | sed -e 's|http://img[0-9].domain.xy/t/[0-9][0-9][0-9]/[0-9][0-9][0-9]/||g' -e "s|.*|&"${var}"|g" >> newFile; 
done < file;
grep -oE "[0-9]*m[0-9]*[0-9]s" newFile | sed -e 's|^|00:|' -e s'|m|:|' -e s'|s||'

this gives me only the output of the numbers not the full line.

00:13:5
00:3:18
00:1:50

and so on

how can i get the full line and just change 13m5s to 00:13:5 ?

if just use sed after the while loop without grep it changes the wrong letters. and puts 00: at the begin of every line.

what is the best way to handle that. i think its be the best to integrate the command in the existing loop. but i have try many differnt variations witout a result.

thx for helping

thx

Upvotes: 0

Views: 225

Answers (2)

iscfrc
iscfrc

Reputation: 421

I broke your code apart in to a few additional pieces to make understanding what was going on easier. Here's the result which I believe is correct:

# Read each field in to separate variables
while IFS='|' read iframe urls time; do
        # Get the first URL from the ';'-separated list
        url="${urls%%;*}"
        # Get the base URL by matching up to the last '/' (and add it back since the match is exclusive)
        base_url="${url%/*}"'/'

        # Remove the base URL from the list of full URLs so only the filenames are left
        files="${urls//$base_url/}"

        # Parse the minute and second out from the '##m#s' string
        IFS='ms' read min sec <<<"$time"

        # Print the new line - note the numeric formatting in the third column
        printf '%s|%s|00:%02d:%02d|%s\n' "$iframe" "$files" "$min" "$sec" "$base_url"
done <file

The lines that answer your specific request about how to turn 13m5s in to 00:13:05 are these two:

IFS='ms' read min sec <<<"$time"

printf '%s|%s|00:%02d:%02d|%s\n' "$iframe" "$files" "$min" "$sec" "$base_url"

The read line uses IFS to tell it to split on the characters m or s making it able to easily read the minute and second variables.

The printf line, with 00:%02d:%02d specifically, formats the $min and $sec variables as zero-padded two-digit numbers.

Upvotes: 1

Barmar
Barmar

Reputation: 781245

grep only outputs the lines that match the expression. Use sed's built-in line matching to restrict the substitution to certain lines:

sed '/[0-9]*m[0-9]*[0-9]s/{s|^|00:|;s|m|:|;s'|s||;}'

or maybe this:

sed 's/\([0-9]*\)m\([0-9]*[0-9]\)s/00:\1:\2/'

Upvotes: 1

Related Questions