1

I have an input file called "namelist.txt" with email addresses:

[email protected]
[email protected]
[email protected]

I am trying to read the addresses from this input file and inserting their values into an output file (newfile.txt) that should read:

user "[email protected]" with pass"pass" is "[email protected]" here
user "[email protected]" with pass"pass" is "[email protected]" here
user "[email protected]" with pass"pass" is "[email protected]" here

The closest I have come is by using awk:

awk '{print "user $0 there with pass"pass" is user $0 here"}' < namelist.txt > newfile.txt

This however prints the following:

user $0 there with pass is user $0 here 
user $0 there with pass is user $0 here
user $0 there with pass is user $0 here

This doesn't print the variable values nor the "pass" value.

I don't know where I am going wrong or whether I am even on the right track, so any advice and / or pointers would be greatly appreciated.

EDIT:

Using:

awk '{print "user \""$0"\" there with pass \"pass\" is user \""$0"\" here}' file

results in the following output on Ubuntu (14.04.2 LTS):

" heree with pass "pass" is user "[email protected]
" heree with pass "pass" is user "[email protected]
" heree with pass "pass" is user "[email protected]

Abovementioned code works fine in a UNIX terminal but not in Ubuntu nor Raspberry Pi.

I am confused as to why the differences between distros.

2 Answers 2

3

The $ variable needs to be outside of quotes and you need to escape the quotes you want to see.

awk '{print "user \""$0"\" there with pass\"pass\" is user \""$0"\" here"}' file

user "[email protected]" there with pass"pass" is user "[email protected]" here
user "[email protected]" there with pass"pass" is user "[email protected]" here
user "[email protected]" there with pass"pass" is user "[email protected]" here
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your quick reply. I think I might have another problem as well though. When I try your answer in an online unix terminal it works perfectly. However when I try it on Ubuntu (or Raspi) I get different results: " heree with pass"pass" is user "[email protected] " heree with pass"pass" is user " [email protected] " heree with pass"pass" is user " [email protected]
Show any problems you are having in formatted text in your post, it is impossible to read and guess the formatting of what you write in comments.
Sorry. I have edited the original post to reflect my attempts of using your answer.
Your input file was created on Windows and so is corrupted with control-Ms at the end of lines. Run dos2unix or similar on it.
For DOS file enablement can put a { sub(/\r$/,"") } clause in your code or if using GAWK you can use RS="\r?\n" -- I do this on scripts I build for work because I want to cleanly handle both DOS and Unix files.
2

You had included the text $0 inside the literal string but in any case when inserting input data into some formatted string for output, it's usually clearest and easiest to enhance in future if you use printf instead of print:

$ awk '{printf "user \"%s\" there with pass \"pass\" is user \"%s\" here\n", $0, $0}' file
user "[email protected]" there with pass "pass" is user "[email protected]" here
user "[email protected]" there with pass "pass" is user "[email protected]" here
user "[email protected]" there with pass "pass" is user "[email protected]" here

and don't use input redirection with awk as that removes the ability for the script to access the input file name. awk is perfectly capable of opening files itself.

1 Comment

Thx for your insight. I value it highly.

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.