awk in shellscript


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk in shellscript
# 1  
Old 02-25-2015
awk to bash variable suddenly stores filessystem data

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:
Code:
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:
Code:
My Information

Now pasting the same command in a shell variable
Code:
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:
Code:
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

Last edited by crumble; 02-25-2015 at 08:23 AM.. Reason: edited a more specific title
# 2  
Old 02-25-2015
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
# 3  
Old 02-25-2015
Quote:
Originally Posted by crumble
Code:
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
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?
# 4  
Old 02-25-2015
I was able to cut the problem to a minimal example:
Let's have a file /tmp/file with
Code:
"* My Information"

Now,
Code:
cat /tmp/file | awk -F '"' '{print $2}'

gives the expected result:
Code:
* My Information

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

gives
Code:
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
# 5  
Old 02-25-2015
Code:
content=$(awk -F '"' '{print $2}' /tmp/file)
echo "${content}"

# 6  
Old 02-25-2015
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 Smilie
# 7  
Old 02-25-2015
That's it Smilie! Thank you very much!
Everything works now as expected.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to add field to diffrent file using shellscript? or awk

hi, would you help me? i have file total.csv "a","e23","f" "b,"34d","g" "c","45f","f" "d","45s","f" count.csv 3 i do this : paste -d',",' total.csv count.csv but the result like this: "a,"e23","f" 3 "b,"34d","g" (1 Reply)
Discussion started by: kivale
1 Replies

2. Shell Programming and Scripting

Help with shellscript

I am new in shell script i want to convert .txt file in the format axsjdijdjjdk to a x s j d i j d j j d k (5 Replies)
Discussion started by: sreejithalokkan
5 Replies

3. Shell Programming and Scripting

Needed shellscript for the following

hi all, i need the shell script for he below requirement i had the input file as a_20121217_035120( frmat is a_date_hhmmss) a_20121217_035128 a_20121217_035456 a_20121217_035767 a_20121217_035178 a_20121217_035189 a_20121217_035220 my output should be a_20121217_035456... (0 Replies)
Discussion started by: hemanthsaikumar
0 Replies

4. Shell Programming and Scripting

if condition in shellscript

Hi All, I tried below code getting error. AD=0 ZERO=0 if then echo "AD is zero select another symbol" fi syntax error near unexpected token `fi' plz help me to solve this error (4 Replies)
Discussion started by: aish11
4 Replies

5. Shell Programming and Scripting

Help with awk in a loop/shellscript

I am trying to write a shell script using awk which will format the output of the "ps -eaf" command by sorting it by user and then listing all of the processes for each user. Here is an example of the output I want: Here is the code I have: #! /bin/sh for I in `ps -eaf h | awk '{print $1}'... (7 Replies)
Discussion started by: Shinsuio
7 Replies

6. Shell Programming and Scripting

Need help with shellscript

Hello. I am a novince at writing shell scripts but here is the question. I have to write a shell script that does the following: Once executed via crontab, the script should do the following: a. get date/time stamp in for format 10-MAR-05 and b. execute shell script my_script.sh (which... (2 Replies)
Discussion started by: jigarlakhani
2 Replies

7. Shell Programming and Scripting

Another shellscript question

Folks; on a unix server I have a mapping file which holds a list mountpoints of all databases and their mountpoints. tab delimited or colon deliminted..I needed to copy the datafiles from the pristine mountpoints to test's mountpoints in this case. I needed to do this by passing sid name using... (18 Replies)
Discussion started by: jigarlakhani
18 Replies

8. Shell Programming and Scripting

Create Shellscript

I am new to UNIX. I got the file from Oracle, with two columns (Table Name and Column Name). I need to create the shell script where the result suppose to include plain text, <table_name>, <Column_name> from the file. Plain text will be the statements to create index in Oracle. something like... (1 Reply)
Discussion started by: newuser100
1 Replies
Login or Register to Ask a Question