Editing files with sed or something similar


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Editing files with sed or something similar
# 1  
Old 05-24-2014
Editing files with sed or something similar

Code:
{
"AFafa": "FAFA","AFafa": "FAFA"
"baseball":"soccer","wrestling":"dancing"
"rhinos":"crocodiles","roles":"foodchain"
}

I need to insert a new line before the closing brackets "}" so that the final output looks like this:

Code:
{
"AFafa": "FAFA","AFafa": "FAFA"
"baseball":"soccer","wrestling":"dancing"
"rhinos":"crocodiles","roles":"foodchain"
"contacts":"groupings","emails":"addresses"
}

i got this far:

Code:
sed 's~}~"contacts":"groupings","emails":"addresses"~g'

any ideas?
# 2  
Old 05-24-2014
Longhand using _builtins_, OSX 10.7.5, default bash terminal...
Code:
#!/bin/bash
# bracket.sh
printf "\nCreate the first part...\n\n"
ifs_str="$IFS"
IFS="}"
echo '{
"AFafa": "FAFA","AFafa": "FAFA"
"baseball":"soccer","wrestling":"dancing"
"rhinos":"crocodiles","roles":"foodchain"
}' > /tmp/file1
text=`cat < /tmp/file1`
echo "$text"
printf "\nNow to build up and convert...\n\n"
# Woring part.
extra_line='"contacts":"groupings","emails":"addresses"'
text_array=($text)
end_text="\n}\n"
text="${text_array[0]}$extra_line$end_text"
printf "$text" > /tmp/file2
# End of working part.
cat < /tmp/file2
IFS="$ifs_str"
exit 0

Results:-
Code:
Last login: Sat May 24 15:59:05 on ttys000
AMIGA:barrywalker~> ./bracket.sh

Create the first part...

{
"AFafa": "FAFA","AFafa": "FAFA"
"baseball":"soccer","wrestling":"dancing"
"rhinos":"crocodiles","roles":"foodchain"
}

Now to build up and convert...

{
"AFafa": "FAFA","AFafa": "FAFA"
"baseball":"soccer","wrestling":"dancing"
"rhinos":"crocodiles","roles":"foodchain"
"contacts":"groupings","emails":"addresses"
}
AMIGA:barrywalker~> _

# 3  
Old 05-24-2014
Or with awk:

Code:
line='"contacts":"groupings","emails":"addresses"'
awk -v line=$line '$0~/}/{print line}1' file

# 4  
Old 05-24-2014
Quote:
Originally Posted by SkySmart
I need to insert a new line before the closing brackets "}" so that the final output looks like this:

...<snip>...

i got this far:

Code:
sed 's~}~"contacts":"groupings","emails":"addresses"~g'

any ideas?
sed has an insert command, i. Use the closing bracket as an address for the insertion.

Regards,
Alister

Last edited by alister; 05-24-2014 at 02:21 PM..
# 5  
Old 05-24-2014
The classic sed needs multi-line
Code:
sed '
/}/i\
"contacts":"groupings","emails":"addresses"
' file


Last edited by MadeInGermany; 05-24-2014 at 10:03 PM..
# 6  
Old 05-25-2014
The follow works on my mac:


Code:
sed  's/}/"contacts":"groupings","emails":"addresses"\'$'\n}/' file

# 7  
Old 05-25-2014
Also try

Code:
$ cat file
{
"AFafa": "FAFA","AFafa": "FAFA"
"baseball":"soccer","wrestling":"dancing"
"rhinos":"crocodiles","roles":"foodchain"
}

Code:
$ awk 'gsub(/}/,new"\n&") + 1' new='"contacts":"groupings","emails":"addresses"' file
{
"AFafa": "FAFA","AFafa": "FAFA"
"baseball":"soccer","wrestling":"dancing"
"rhinos":"crocodiles","roles":"foodchain"
"contacts":"groupings","emails":"addresses"
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Isolate text with sed or similar utility

All, I'm getting a list like the following and I'd like to kill each PID in turn. pid (17797) pid (21748) pid (21754) pid (21704) pid (2199) pid (2159) pid (17809) pid (21769) pid (21778) pid (21715) ... (3 Replies)
Discussion started by: ejianu
3 Replies

2. Answers to Frequently Asked Questions

Why Parsing Can't be Done With sed ( or similar tools)

Regularly we have questions like: i have an XML (C, C++, ...) file with this or that property and i want to extract the content of this or that tag (function, ...). How do i do it in sed? Yes, in some (very limited) cases this is possible, but in general this can't be done. That is: you can do... (0 Replies)
Discussion started by: bakunin
0 Replies

3. UNIX for Dummies Questions & Answers

sed help finding and editing

With sed 1. I need to find a line that contains "DVM" and "73069". 2. I need to insert a double quote at the beginning of the first line of the file. These two have been driving me crazy for the last 45 minutes. Any help would be greatly appreciated. Thanks (3 Replies)
Discussion started by: nlassiter
3 Replies

4. UNIX for Dummies Questions & Answers

sed editing help....

Hello all, I need some help with sed. seems like i cant get through it. So here is what i am trying. when i do ps -ef|grep bla blah ...like below...i get /u01/app/oracle/11g/bin/tnslsnr .... but i want to replace that string with something using sed. So basically i want to get rid of... (3 Replies)
Discussion started by: abdul.irfan2
3 Replies

5. Shell Programming and Scripting

sed or awk editing help

Hi all I use aix (sadly). I've got a file consisting of fields separated by commas, I need a sed or awk command that will delete all spaces between two commas as long as there are only spaces between the commas. eg ,abc, ,sd , ,dr at would become ,abc,,sd ,,dr at I have... (53 Replies)
Discussion started by: mychmose
53 Replies

6. Homework & Coursework Questions

String editing using sed? awk?

1. The problem statement, all variables and given/known data: Problem Statement for project: When an account is created on the CS Unix network, a public html directory is created in the account's home directory. A default web page is put into that directory. Some users replace or... (13 Replies)
Discussion started by: peage1475
13 Replies

7. Shell Programming and Scripting

Editing Commas in a textfile using sed

Hi guys task removing the last commas of 5th and 6th columns. The bug in the script is causing effect because of whitespaces around commas. I tried to delete white spaces first and running the above script. but still some where getting the results wrong. I already have a script to do this... (12 Replies)
Discussion started by: repinementer
12 Replies

8. Shell Programming and Scripting

Using sed (or similar) to rename variable headings

Hello, I'm rather new to the world of regular expressions and sed, though am excited by its possibilities. I have a particular task I'd like to achieve, and have googled the topic quite a bit. However, having found some codes that perform a task very similar to what I'd like to do, I can't for... (2 Replies)
Discussion started by: redseventyseven
2 Replies

9. Shell Programming and Scripting

awk, sed or similar log repair help

I have a log file that for some reason, once or two time a month, line foods are missing. This log is generated from vmstat everyminute. I dont know why sometimes it does this. Each line in the log should have 18 columns separated by one or more spaces. Good Log: (not actual log) 1 1... (8 Replies)
Discussion started by: Ikon
8 Replies

10. Shell Programming and Scripting

Editing File using awk/sed

Hello Awk Gurus, Can anyone of you help me with the below problem. I have got a file having data in below format pmFaultyTransportBlocks ----------------------- 9842993 pmFrmNoOfDiscRachFrames ----------------------- NULL pmNoRecRandomAccSuccess -----------------------... (4 Replies)
Discussion started by: Mohammed
4 Replies
Login or Register to Ask a Question

Featured Tech Videos