When i execute 2.sh i should get the formatted printed output
I have tried the following codes;
awk '/column/ { print <> }' format.txt > we
where the <> is awk 'BEGIN .....
The problem with this command is I cannot get the lines above or below the search.
i.e only the lines containng "column" will be replaced and written to 2.sh
If i use sed, I cannot get the fileds from the file 1.txt
i.e; In order to get the "%1s" "%100s" from file 1.txt is not possible.
Is their any way we can overcome these two problems ?
Understanding the context of this problem would probably help. Your first awk script specifically looks only for lines containing "column"; if that's not what you want, why do you do that?
Getting a number which is always the second field on a line with sed is not at all impossible.
sed -e 's/^[^ ]* *\([1-9][0-9]*\), *\("[^"]*"\),.*/"%\1s", \2/'
Franklin's code simply removes the commas and the double quotes (from between the fields, and around the third field, but for simplicity anywhere else too), then uses the selected fields in a printf directly.
Am specifically looking for lines that are beginning with "column" and replacing those lines with an " awk" command
In Short if i have a line :
column 1, "cat",
I need to replace this line with :
awk 'BEGIN { printf ("%1s","cat")}' ( given that no change should occur to the other lines in the file including the line numbers )
Still a bigger context would be useful for understanding the problem and your attempt at a solution, at it seems highly inefficient to create umpteen simple awk scripts with only a BEGIN part.
sed -e 's/^[^ ]* *\([1-9][0-9]*\), *\("[^"]*"\),.*/"%\1s", \2/'
The above code seems to work
But I have some doubts here .
Where is this code searching for "column" ( though it worked on the first column statement ) ?
I have many statements beginning with "column". The sed here works for the first found "column". How can i make it work for all the column?
I tried using the below code as u have given :
cat 1.txt | sed -e 's/^[^ ]* *\([1-9][0-9]\), *\("[^"]*"\),./awk '"'"'BEGIN {printf("%\1s", \2)}'"'"'>> 2.sh/' > temp.txt
This worked for the first occurance of "column"
Where as when i used the code given below, there was no change in 1.txt and 2.sh.
It's not searching for the first occurrence. The updated code contains precisely the modification to only match lines which start with "column". Probably there is something in the regular expression which doesn't match your examples exactly. Worked on the samples I copy/pasted from above.
fish in the water
print
column 1, "cat",
column 24, "dog",
column 100, "rat"hill and jill
run another hat
print column 23, "elephant"I commit to work
print
column 45, "friend"
column 34, " mother"
The junk stuff I added are other lines of the file 1.txt
The sed script I wrote before assumes -- based on the examples you gave earlier -- that "column" would always be right at the start of a line. If it's not really, you want to add a " *" after the "^". Lesson: don't obfuscate your examples too much.
But Franklin's solution will directly put the script to give the output which is not want i need.
1.txt > temp.sh > 2.sh
when 2.sh runs i shud get the output not when temp.sh runs.
I need the solution to be written in temp.sh so that it access 1.txt, replace certain lines and writes to 2.sh. Then am suppose to run 2.sh to get the formatted printing.
franklin's solution directly jumps to the output. i dont get a 2.sh !!!
Am still facing problems with this .
Am thinking of another logic.
Keeping the same file format ( I have writeen in bold -- scroll up), can I search for teh patterns "print" and "column" using a single grep (or any other search command ) from teh file 1.txt and write it to "temp.txt"
after using the above command and a bit of tuning i made the file format as below:
print
# Print first some lines #
column 1, "cat",
column 9, rat,
column 56, goat
# this is a comment statement #
print column 46, naming function
print heaven
print hell
print
column 23, market"&&&",
column 30, fill
######
print
column 55, descrition,
Can u help mw to get some variables as below:
var1="print column 1, "cat", column 9, rat, column 56, goat"
var2="print column 46, naming function"
var3="print heaven"
-----
var5="print column 23, market"&&&",column 30, fill"
dont want the comment statements. And i can remove them ..In short,i need to select from the first" print " to the next print(exclude) and store that in a variable.
I can do the storing but cutting is not successful. I tried using awk but that too didnot give the desired o/p