LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-24-2017, 11:06 AM   #1
hruday
Member
 
Registered: Jun 2015
Posts: 88

Rep: Reputation: Disabled
Read first line in a file and append it to first occurrence of the search string and Repeat until EOF


I have a text file with following lines in following format

Code:
"abc": "xyz",
"qwe": "uva",
"asd": "lkj",

I have a file with following lines in a text file in following format

"abc": "xyz",
"qwe": "uva",
"asd": "lkj",
And a json file in following format
Code:
[
{
    "svn-user-id": "passwd"
    "solution-id": 0,
    "cronos-id": "1",
    "solution-state": "active",
  },
{
    "svn-user-id": "passwd"
    "solution-id": 1,
    "cronos-id": "1",
    "solution-state": "active",
  },
{
    "svn-user-id": "passwd"
    "solution-id": 2,
    "cronos-id": "1",
    "solution-state": "active",
  }
]
Now i want output of json as follows
Code:
[
{
        "svn-user-id": "passwd"
        "solution-id": 0,
         "abc": "xyz",
        "cronos-id": "1",
        "solution-state": "active",
      },
    {
        "svn-user-id": "passwd"
        "solution-id": 1,
         "qwe": "uva",
        "cronos-id": "1",
        "solution-state": "active",
      },
    {
        "svn-user-id": "passwd"
        "solution-id": 2,
         "asd": "lkj",
        "cronos-id": "1",
        "solution-state": "active",
      }
]
So how can achieve this using bash?

I've tried with following but it only appends last line of the file.

Code:
#!/bin/bash
set -x
file=names.txt #file has list of lines as described above
IFS=$'\n'

for l in `cat $file`
        do
        echo $l 
        sed '/"solution-id": 1,/a \'"$l"'' sample.json # json file as described above
        done
 
Old 11-24-2017, 12:07 PM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,325

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
How big is the size of file with "abc": "xyz" etc...?


Also, is the second file exactly like your example?
"passwd" value, same for each block and solution-id, just incremented by 1 each time, cronos-id same in each block etc

Last edited by keefaz; 11-24-2017 at 12:12 PM.
 
Old 11-25-2017, 12:19 AM   #3
hruday
Member
 
Registered: Jun 2015
Posts: 88

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by keefaz View Post
How big is the size of file with "abc": "xyz" etc...?

file has exactly 10 different lines.

Also, is the second file exactly like your example?
"passwd" value, same for each block and solution-id, just incremented by 1 each time, cronos-id same in each block etc
==> Yes json file is exactly as in the example.
 
Old 11-25-2017, 06:49 AM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,325

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
So you could divide the json block into 2 parts, print the first part, the line to insert, then the second part
Add a variable that get incremented by 1 every loop iteration for solution-id value

Code:
set counter to 0
[loop that reads first file line by line]
print first json part with solution-id value set to counter value
print read line
print json second part
increment counter by 1
[end of loop]
Redirect output of script to a file to save it

Ask if you need more help
 
Old 11-25-2017, 07:48 AM   #5
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 5,627

Rep: Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198
You have asked about a bash solution, but added a tag for sed, so perhaps you are open to alternative solutions to run from the CLI.
Code:
awk '/cronos-id/ {getline s <"text.txt";print "    " s};{print}' json_file

Last edited by allend; 11-25-2017 at 07:53 AM.
 
Old 11-25-2017, 05:53 PM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,737

Rep: Reputation: 789Reputation: 789Reputation: 789Reputation: 789Reputation: 789Reputation: 789Reputation: 789
In shell, using an extra descriptor (the standard method of reading from a further file)
Code:
while IFS="" read line
do
  case $line in
  (*'"cronos-id"'*)
    read s <&3
    printf "    %s\n" "$s"
  ;;
  esac
  printf "%s\n" "$line"
done <"json_file" 3<"text.txt"

Last edited by MadeInGermany; 11-29-2017 at 02:32 PM. Reason: added IFS=""
 
Old 11-26-2017, 10:42 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,848

Rep: Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013
Code:
IFS=$'\n'
for l in `cat $file`
...
Don't use for loops on file contents. Use while read loops instead.
http://mywiki.wooledge.org/DontReadLinesWithFor

It's also not a good idea to mess around IFS unless you really need it for word-splitting -- and you know what you are doing. Learn how to properly handle and quote strings instead.
http://mywiki.wooledge.org/Quotes

And `backticks` are ancient relics of the past. All modern shells use $(..) instead for command substitution.
http://mywiki.wooledge.org/BashFAQ/082
 
  


Reply

Tags
bash, linux, scripting, sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[Close] search for exact string on file and delete that line pedropt Programming 3 07-11-2017 03:36 PM
LXer: How to search and replace a multi-line string in a file LXer Syndicated Linux News 0 02-25-2016 05:00 AM
read line from a file and check the string rajachan Programming 2 01-18-2013 03:23 AM
[SOLVED] sed append string in variable to last line of file. SilversleevesX Linux - Newbie 7 11-27-2011 11:13 PM
How to read string in 1 file and search in another file? ameyapandit Programming 7 07-11-2008 12:05 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:06 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