awk in shellscript

Dear Members,

I have the following situation I do not understand:
I have a large json encoded file which I need to grep and afterwards want to extract specific information.
I use this command to to that:

cat /tmp/file | awk -F '"fields":' '{print $2}' | awk -F '"description":' '{print $4}' | awk -F ',' '{print $1}' | awk -F '"' '{print $1}'

This works fine and give me the information I need:

My Information

Now pasting the same command in a shell variable

content=`cat /tmp/file | awk -F '"fields":' '{print $2}' | awk -F '"description":' '{print $4}' | awk -F ',' '{print $1}' | awk -F '"' '{print $1}'`

suddenly gives me all files and folders in that directory followed by the information I'm looking for:

folder1 folder2 file1 file2 My Information

Does anyone have a clue what could cause such behavior and can give some advice on how to get rid of the file information?

Thank you very much in advance
Crumble

Hello Crumble,

Welcome to forum, could you please show us the complete input and expected output, it may help us to understand the requirement better.

Thanks,
R. Singh

Are you running it from the command-line, or in a script? If it's in a script, can you show us more of it, and how are you running it?

Also, what's in /tmp/file?

I was able to cut the problem to a minimal example:
Let's have a file /tmp/file with

"* My Information"

Now,

cat /tmp/file | awk -F '"' '{print $2}'

gives the expected result:

* My Information

Yet,

content=`cat /tmp/file | awk -F '"' '{print $2}'`

gives

file1 file2 My Information

The problem is the asterisk (*). If the asterisk is not present, it does not get substituted with the files and directories.
I assume I have to escape those characters but I cannot find a table of characters which have to be escaped. Maybe there is another way using other quote types or something else.
Any further help is very much appreciated!

Crumble

content=$(awk -F '"' '{print $2}' /tmp/file)
echo "${content}"

I suspect it's just the way you're displaying $content.

echo * will be globbed by the shell before it display anything (which results in the list of filenames). Double-quotes should fix it (i.e. echo "${var}" ).

EDIT: As vgersh99 has pointed out :slight_smile:

That's it :)! Thank you very much!
Everything works now as expected.