awk printing output to new line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk printing output to new line
# 1  
Old 01-31-2017
awk printing output to new line

Hi

I have a file profile.txt with the below input:
Code:
{"atgUserId":"736f14c4-eda2-4531-9d40-9de4d6d1fb0f","firstName":"donna","lastName":"biehler","email":"schoolathome42@live.com","receiveEmail":"y
es"},
{"atgUserId":"c3716baf-9bf8-42da-8a44-a13fff68d20f","firstName":"Gilberto Ramon","lastName":"Trevino Fuerte","email":"gtrevino@dfasc.com","recei
veEmail":"yes"}

I am writing a shell script in for to print the lines one by one from profile.txt including braces via:

Code:
for value in $(awk -F"{|}" '{printf "{"$2"}"}' profile.txt) {; do  echo $value; done

But it is giving me a broken output as;

Code:
{"atgUserId":"736f14c4-eda2-4531-9d40-9de4d6d1fb0f","firstName":"donna","lastName":"biehler","email":"schoolathome42@live.com","receiveEmail":"yes"}{"atgUserId":"c3716baf-9bf8-42da-8a44-a13fff68d20f","firstName":"Gilberto
Ramon","lastName":"Trevino
Fuerte","email":"gtrevino@dfasc.com","receiveEmail":"yes"}
{

Note that the Gilberto and Ramon have separated? How to resolve this?
# 2  
Old 01-31-2017
what's the desired out give your sample input?
# 3  
Old 01-31-2017
Desired output should be:

Code:
{"atgUserId":"736f14c4-eda2-4531-9d40-9de4d6d1fb0f","firstName":"donna","lastName":"biehler","email":"schoolathome42@live.com","receiveEmail":"yes"}{"atgUserId":"c3716baf-9bf8-42da-8a44-a13fff68d20f","firstName":"Gilberto Ramon","lastName":"Trevino Fuerte","email":"gtrevino@dfasc.com","receiveEmail":"yes"}

Instead of the below one:
Code:
{"atgUserId":"736f14c4-eda2-4531-9d40-9de4d6d1fb0f","firstName":"donna","lastName":"biehler","email":"schoolathome42@live.com","receiveEmail":"yes"}{"atgUserId":"c3716baf-9bf8-42da-8a44-a13fff68d20f","firstName":"Gilberto
Ramon","lastName":"Trevino
Fuerte","email":"gtrevino@dfasc.com","receiveEmail":"yes"}

Whenever there is a space between a name it breaks the line Smilie
# 4  
Old 01-31-2017
So you just want to remove all newlines?

Code:
tr -d '\n' < infile > outfile

# 5  
Old 01-31-2017
I don't quite see how the name gets broken into 2 parts given your sample input, but try this:

Code:
awk '{printf substr($0,1, length-1)}END{printf ORS}' myFile

---------- Post updated at 03:48 PM ---------- Previous update was at 03:47 PM ----------

Quote:
Originally Posted by Corona688
So you just want to remove all newlines?

Code:
tr -d '\n' < infile > outfile

and remove the trailing , (I think)
# 6  
Old 01-31-2017
Thanks to both of you...

I got the answer as to why it was splitting.

By default, a Bash for loop splits on all whitespace. You can override that by setting the IFS variable:
Code:
IFS=$'\n'
for i in `cat r.txt`; do echo "$i"; done
unset IFS

Source:

osx - in bash, "for;do echo;done" splits lines at spaces - Stack Overflow

IFS stands for Input Internal Field Separator - it's a character that separates fields.

Code:
IFS=$'\n'       # make newlines the only separator


Last edited by ankur328; 01-31-2017 at 05:10 PM..
# 7  
Old 01-31-2017
Quote:
Originally Posted by ankur328
Thanks to both of you...

I got the answer as to why it was splitting.

By default, a Bash for loop splits on all whitespace.
Your question did not involve a bash for loop. Had you showed what you were actually doing, we might have guessed.

You shouldn't be doing that anyway. That's a useless use of cat, a dangerous use of backticks, and waste of memory (slurping an entire file into memory to deal with it). You didn't find the shell script construct actually intended for this job:

Code:
while read ENTIRELINE
do
        echo "line read: $LINE"
done < inputfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk printing leading tab in output

The awk below executes and produces the current output. it skips the header in row 1 and prints $4,$5,$6 and then adds the header row back. The problem is that it keeps the tailing tab and prints it in front of $1. I could add a pipe to remove the tab, but is there a better way to do it with on... (7 Replies)
Discussion started by: cmccabe
7 Replies

2. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

3. Shell Programming and Scripting

Help! Printing out CSV output from awk Pattern Match

Hi, I need to search for a word using Awk and print out the line the word is in and every line after the search phrase until I hit this #------------. Then I need to send it to a csv file. So basically the input file format is like this:... (1 Reply)
Discussion started by: An0mander
1 Replies

4. UNIX for Dummies Questions & Answers

Using awk to find max and printing entire line

Hi folks, I am very new to awk. I have what is probably a very simple question. I'm trying to get the max value of column 1, but also print column 2. My data looks like this: 0.044|2000-02-03 14:00:00 5.23|2000-02-03 05:45:00 5.26|2000-02-03 11:15:00 0|2000-02-01 18:30:00 So in this case... (2 Replies)
Discussion started by: amandarobe
2 Replies

5. Shell Programming and Scripting

Matching and printing line with awk

Hi there, I'm trying to use awk to print out the entire line that contains a match to a certain regex and then append some text,plus the match to the end of the line. So far I have: awk -F: '{print "RG:Z:" $2}' file Which prints out the match I want plus the additional text, but I'm stuck... (3 Replies)
Discussion started by: jim_lad
3 Replies

6. Shell Programming and Scripting

Maybe by AWK: printing help diagonal matrix characters into line

Hi Experts, I want to print this charts diagonal data into straight lines. This is a matrix 24X24 Horizontal and vertical. - I want to print all the diagonal cutting characters into straight line: Data: E F S S A H A L L A T M C N O T S O B O D U Q H I W I B N L O C N I L N L A N S I N... (9 Replies)
Discussion started by: rveri
9 Replies

7. Shell Programming and Scripting

How to use awk for printing line from database table?

Hi , I have inserted some records in a table having column "value1 varchar2(4000)" and want to spool in a file. I have written as below set echo off set feed off set hea off set wra off set lin 500 spo temp_table and fired select query as below select value1 from temp_table; spo... (6 Replies)
Discussion started by: CaapAjayShukla
6 Replies

8. Shell Programming and Scripting

printing portion of the output usind sed/awk

friends, i am a newbie in scripting. could someone help me in selecting only the last column of below ps command output ? mqm 14 16466 0 Sep 15 ? 0:01 /opt/mqm/bin/runmqlsr -r -m QMGR.INBOUNDSSL -t TCP -p 1415 -i 5.1.26.5 mqm 12 16700 0 Sep 15 ? 0:00... (4 Replies)
Discussion started by: unahb1
4 Replies

9. Shell Programming and Scripting

Printing a specific line using AWK

Hi, I have a script that fetches only specific information from fcinfo command. Below is a portion of the script. #!/usr/bin/ksh set -x HBA_COUNT=`sudo fcinfo hba-port | grep -i state | awk 'END{print NR}'` echo "$HBA_COUNT HBAs exist" echo '........' INDEX=1 while $INDEX -le... (2 Replies)
Discussion started by: jake_won
2 Replies

10. Shell Programming and Scripting

printing a line number using awk

Hi Chaps, I'm trying to print the line number of a comma delimited file where the second field in the line is blank using AWK. Here is the code I have so far where am I going wrong. It is the last column in the file. nawk -v x==0 'BEGIN {FS=",";OFS=","} x++ if ($2 == " ") print $x' bob.tst ... (3 Replies)
Discussion started by: rjsha1
3 Replies
Login or Register to Ask a Question