1

because i haven't found a solution on google or the searchfunction i will ask here.

Here is my code :

Send="last -n 1 $1 | awk '{ print $1 " " $2  }'"

My problem is, that my shell script is using parameters.

When i'm calling my script:

myScript hello world

Then my awk-Command looks like

awk '{ print hello " " world }'

But how could I avoid this? is there a way?

Because this is a part of a project, i couldn't post more code. ;/

2
  • What do you do with your $Send variable? Commented Nov 27, 2012 at 16:05
  • This is for the identification of a user, don't to send duplicate messages ;) Maybe the name is a bit confuse. Commented Nov 27, 2012 at 16:30

1 Answer 1

1
  1. first change the outer "'s to $() so: send=$(last -n 1 $1 | awk '{print $1 " " $2}')

  2. use the FS (field separator) variable which defaults to space in awk instead of " " for a space so: send=$(last -n 1 $1 | awk '{print $1 FS $2}')

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

5 Comments

Replace FS with , as that's the purpose of OFS.
both work. an argument can be made that , is less to type when compared to FS, but my preference is for FS. As it helps me logically make the distinction, whereas a comma can be easily overlooked. Especially when reviewing older code. Preference really.
If you prefer $1 FS $2 to $1, $2, I would argue that you ought to use $1 OFS $2.
@EdMorton and @WilliamPursell, I politely disagree on OFS or , Over FS. That would work well for this case, where OFS or ',' would input a space, but I use FS explicitly as the field separator. I make mention of that in my answer. so if it was changed such as: awk -F- '{print $1 OFS $2}' you would not get the same as if you used FS. If you want space, use a comma, but FS is more robust in this case.
(cannot edit the above) -- There are cases where you may want to use the OFS, and those are when you are setting the Output Field Separator explicitly to something other then the FS, but sometimes you just want to remove or transpose a col using awk, it is a bit superfluous to set the OFS when using the FS is sufficient. Which, brings me back to this being a matter of preference. My answer indicated FS was being used as the Field Separator.

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.