LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-19-2021, 12:44 PM   #1
rootaccess
Member
 
Registered: Mar 2012
Posts: 311

Rep: Reputation: Disabled
Add column from another file into specific area between quotes


I have a list of rows that look like this.

Code:
{data, {"data", "data-that-needs-to-be-changed"}},
The 3rd column is what I need to change. I have that 3rd column solo in a file from a previous
Code:
awk -F\" '{print $4}' file
which just prints the area defined as
Code:
data-that-needs-to-be-changed
between the quotes.


Thanks.
 
Old 11-19-2021, 12:50 PM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,041

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
is this a json file?
You need to use a tool which can handle that properly (for example python). awk is definitely unsafe in such cases.
 
Old 11-19-2021, 02:09 PM   #3
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,678

Rep: Reputation: Disabled
Field variables in awk can be assigned to
Code:
 awk -F\" -vOFS=\" '$4="new-data"' file
But as pan64 said, if this is indeed JSON then better handle it with a JSON parser. If this were part of a shell script, jq would be my tool of choice.

Last edited by shruggy; 11-19-2021 at 03:15 PM.
 
Old 11-19-2021, 10:28 PM   #4
rootaccess
Member
 
Registered: Mar 2012
Posts: 311

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by shruggy View Post
Field variables in awk can be assigned to
Code:
 awk -F\" -vOFS=\" '$4="new-data"' file
But as pan64 said, if this is indeed JSON then better handle it with a JSON parser. If this were part of a shell script, jq would be my tool of choice.

Wasnt able to do much with that since new-data is data that is a file full of rows that needs to modify that. but i ended up just using paste with that column. i printed just the 2 columns up to the 3rd quote, then i pasted the column with the new data, then i just appended a
Code:
"}},
to the end of each row. simple as that. thank you.

Last edited by rootaccess; 11-19-2021 at 10:29 PM.
 
Old 11-19-2021, 10:44 PM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,359
Blog Entries: 3

Rep: Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767
Right. But if the format of that data is JSON then there are specific tools which will be easier to work with, reduce or eliminate formatting errors, and be more flexible and less brittle.
 
Old 11-27-2021, 08:29 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,832

Rep: Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219
With shell built-ins
Code:
#!/bin/sh
q=\"
while
  IFS="$q" read f1 f2 f3 f4 f5 <&3
do
  read nd <&4
  echo "$f1$q$f2$q$f3$q$nd$q$f5"
done 3<file 4<new-data
 
  


Reply

Tags
awk, bash, json, sed



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Difference between echo of file within double quotes and without double quotes ankitpandey Programming 2 01-11-2013 09:02 AM
Yet Another Bash Quotes Within Quotes Issue tboyer Linux - Software 17 11-03-2012 11:17 AM
[SOLVED] add - (minus) in front of a column with value condition of another column cazanadrian Linux - Newbie 1 10-30-2012 01:51 PM
Problems with quotes and double quotes Andruha Slackware 6 01-02-2010 04:44 PM
Using single quotes vs double quotes in PHP strings vharishankar Programming 6 07-11-2005 11:41 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration