0

I have a text file with unknown length. There are two values on each line:

VALUE1[SPACE]VALUE2

Now I have to get another (or the same) file with a new List like:

0.0.0.0/rep/com/bla/blub/VALUE1/VALUE2/VALUE1-VALUE2.zip

...for each line I have in the list with the two values. How can I do this?

4 Answers 4

1

This is a good usecase for awk (Updated to take care of the parantheses):

awk -F" " '{ gsub("\\(", "", $1); gsub("\\)", "", $2);print "0.0.0.0/rep/com/bla/blub/"$1"/"$2"/"$1"-"$2".zip"}' test.txt > yournewfile.txt

This will split each line with a space, replace the opening parentheses in your first token $1 and replace the closing parentheses in your second toke $2 and then use values in their respective tokens $1 and $2 in the string you are outputing with print

Sign up to request clarification or add additional context in comments.

3 Comments

this only works for first value set or not ? i need to edit line by line to get for each line a link with the values from this line :/
This will go through every line in your input file and create a new line in your output file.
Please undelete your recent SQL answer. It could still be helpful to others besides the OP (just be clear that it only works in SQL Server 2012 and up).
0

I assume that VALUE1 and VALUE2 contain no spaces, otherwise the first definition becomes ambigous. With this hypothesis, you can split a line at the first space using cut and compose the filename variable using string interpolation:

cat txt-File | while read VALUE1 VALUE2 _; do
    filename="0.0.0.0/rep/com/bla/blub/$VALUE1/$VALUE2/$VALUE1-$VALUE2.zip"

    # Do something with filename...
    stat "$filename"
done

5 Comments

while read VALUE1 VALUE2 _; do ... done <file would avoid pesky external processes and be somewhat more idiomatic.
uff, you can simplify this very much: cat file | while is not necessary, you can say while ... done < file. Also, read can read more than one argument by saying while read value1 value2 ...
I prefer the leading cat file because it makes obvious what the loop is processing, without having to scroll to the bottom. Fixed the other thing.
@StefanoSanfilippo: a shame you can't put the <file at the front of the loop like you can with a regular single command. It would be clearer.
0

The format of your input data file is (VALUE1 VALUE2).. You first remove the initial (, and latter ). There are many ways to do this, here is one

sed 's/(//g' yourfile | sed 's/).//g' 

You can then pass this over to awk. Here $1 corresponds to the first column, and $2 to the second:

sed 's/(//g' yourfile | sed 's/).//g' | awk '{print "0.0.0.0/rep/com/bla/blub/"$1"/"$2"/"$1"-"$2".zip"}'

1 Comment

I suggest you just try it out for yourself ;-)
0

I would use sed for this:

sed $'s,^[ \t]*\([^ \t]*\)[ \t][ \t]*\([^ \t]*\).*$,0.0.0.0/rep/com/bla/blub/\1/\2/\1-\2.zip,' filename >new-filename

You could modify the original file in place if you prefer by adding -i.bak: sed -i.bak $'s,...,'.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.