LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 10-17-2015, 02:09 PM   #1
isuuck
LQ Newbie
 
Registered: Oct 2015
Posts: 4

Rep: Reputation: Disabled
Sed a piped awk


Hello guys, i have a code

b=$(echo "$p" | awk -F":" '{ for (i=2; i<=NF; i++) printf "<file>"$i"</file>" }')
i want to take the output and put it into a file after a match or pattern. I also am unable to get \n to work. if i use \r\n then my other outputs don't print. I don't understand

so i was planning using sed -i "/<index>/a$b" insertFilehere
but i keep getting an error message.
is there any a way or any other way to get the output of b into the file after a match?
thank you in advance.

Last edited by isuuck; 10-17-2015 at 02:18 PM.
 
Old 10-17-2015, 02:25 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Hi and welcome to LQ.
What error do you get and what is the output of b? It might be something to do with escaping special characters.
 
Old 10-17-2015, 03:12 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,253

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
Talk about a confusing question. Let us start from the top shall we:

1. Please [code][/code] tags around code or data

2. What is in the variable 'p'?

3. You talk plenty about a 'match', but there is no testing for any match at all in the awk statement

4. You mention '\r\n', does the data stored in 'p' variable come originally from a file written in Windows?? If not, I see no reason this was introduced.

5. What is the expected data in 'b' variable?

6. As above, what error message are you actually getting?

As you can see, a poorly written question only leads to questions, not solutions.
 
Old 10-17-2015, 03:29 PM   #4
isuuck
LQ Newbie
 
Registered: Oct 2015
Posts: 4

Original Poster
Rep: Reputation: Disabled
Okay sorry for the confusing question.

the variable p was from
while read p; do

done <afile

after experimenting a little bit i got the could to work.

The awk statement would pasrse at ":"
so it would have been
b=$( echo $p | awk -F":" '{(for (i=2; 1<=NF; i++) prinf "<file>$i</file>")}')

i put "\r\n" before in b which made it b=$( echo $p | awk -F":" '{(for (i=2; 1<=NF; i++) prinf "<file>$i</file>\r\n")}')

part of the code was main code was

if [ "sometext" == "index" ]
b=$( echo $p | awk -F":" '{(for (i=2; 1<=NF; i++) prinf "<file>$i</file>")}')
sed -i "/<index>/a$b" somefile
fi

again p = the line being read.
The sed output is
<file>somefile1</file>somefile2</file><file>somefile3</file>
i want the output to look like
<file>somefile1</file>
<file>somefile2</file>
<file>somefile3</file>

and tabbeed if possible.
so the sed put the output in somefile under the match which in this case is <index>
if i just /r/n i get an error from sed saying sed -e : unknown 'f' command

Last edited by isuuck; 10-17-2015 at 03:38 PM.
 
Old 10-17-2015, 03:54 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,253

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
So not a bad explanation, although you missed point number 1!!!

Are you not able to tell us what the data in 'afile' looks like so we can see the data in 'p'?

Yes your error is not unusual, may I ask why you put '\r\n' into the 'b' variable? This would only be needed under Windows as a line ending so serves no purpose, that I can see, in a bash script.

If you want the output to look a particular way we will need more details on the original data and whether or not the final file is to be used on Windows or linux??
 
Old 10-17-2015, 04:24 PM   #6
isuuck
LQ Newbie
 
Registered: Oct 2015
Posts: 4

Original Poster
Rep: Reputation: Disabled
sorry.
so variable p is

while read p; do

if [ "sometext" == "index" ]
b=$( echo $p | awk -F":" '{(for (i=2; 1<=NF; i++) prinf "<file>$i</file>")}')
sed -i "/<index>/a$b" somefile
fi

done <file
the script is about 176 lines long right now.
the part that needs fixing is

while read p; do
if [ "sometext" == "index" ]
b=$( echo $p | awk -F":" '{(for (i=2; 1<=NF; i++) prinf "<file>$i</file>")}')
sed -i "/<index>/a$b" somefile
fi
done <file
The file that is being read is a README file. I know the contents of the file so i will parse it with awk -F ":" by line.
the file being parsed looks like

index:file1.2:file2.2:file3.3:file.4

the out put file looks like

<index>
</index>

<required>
</required>
the output right now is
<file>somefile1</file>somefile2</file><file>somefile3</file>

i want the output to look like this in the file that it will send the output to
<index>
<file>somefile1</file>
<file>somefile2</file>
<file>somefile3</file>
</index>

instead of
<index>
<file>somefile1</file><file>somefile2</file><file>somefile3</file>
</index>
 
Old 10-17-2015, 09:43 PM   #7
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,433

Rep: Reputation: 1352Reputation: 1352Reputation: 1352Reputation: 1352Reputation: 1352Reputation: 1352Reputation: 1352Reputation: 1352Reputation: 1352Reputation: 1352
What you want to achieve can be done within bash using an array, without calling awk or sed.
Assuming that the original file is using the colon character as a field delimiter,
Code:
#!/bin/bash

while IFS=: read -a arr; do
  if [ "${arr[0]}" = "index" ]; then
    echo "<index>"
    for ((i=1; i < "${#arr[@]}"; i++)) ; do
      echo "<file>${arr[i]}</file>";
    done
    echo "</index>"
  fi
done < inputfile

Last edited by allend; 10-17-2015 at 09:49 PM.
 
Old 10-18-2015, 01:48 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,253

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
Based on the file structures (STILL NO CODE TAGS):
Code:
#!/usr/bin/env bash

IFS=":" read -a arr <input_file

if [[ "${arr[0]}" == "index" ]]
then
	sed -i "/<index/a $(printf "<file>%s</file>\\\\n" "${arr[@]:1}")" output_file
fi

Last edited by grail; 10-18-2015 at 06:21 AM.
 
Old 10-18-2015, 04:40 AM   #9
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,122

Rep: Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270
grail, a % was lost in your post (I know that is the engine, but anyhow it is not ok):
sed -i "/<index/a $(printf "<file>%s</file>\\\\n" "${arr[@]:1}")"
 
Old 10-18-2015, 02:04 PM   #10
isuuck
LQ Newbie
 
Registered: Oct 2015
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thank you all for your help!!
 
  


Reply


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
[SOLVED] Help with sed/awk maddyfreaks Linux - Newbie 11 10-03-2015 04:43 PM
[SOLVED] awk or sed help cli Linux - Newbie 7 03-05-2015 01:43 AM
[SOLVED] Using a variable with Piped AWK commands ampsys Programming 4 01-24-2014 07:15 PM
Help with awk or sed. tuxtutorials Linux - Software 3 07-23-2009 04:26 PM
awk or sed help cmontr Programming 16 05-14-2008 11:59 AM


All times are GMT -5. The time now is 12:48 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration