ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi,
I tried to get a block of lines in awk, but unfortunately it returns output of one line only. I don't state the code here, because it's too short and too poor. What exactly I wanted to do: from file "/boot/grub/menu.lst" get blocks of lines, starting by title and ending by \n\n
Hi. What do you mean for 'as one row'? Arrays in bash are not multi-dimensional (you can only mimic them using indirect variable reference). Maybe do you want to assign one block of lines to a single element of the array?
#!/bin/bash
readonly false=
readonly true=true
in_stanza_flag=$false
i=-1
while read line
do
case $line in
title* )
let i++
in_stanza_flag=$true
;;
* )
esac
[[ $in_stanza_flag ]] && array[i]=${array[i]}$line$'\n'
done < /boot/grub/menu.lst
for (( i=0; i<${#array[*]}; i++ ))
do
echo "Stanza $i"$'\n'"${array[i]}"
done
If there is stuff after the end of the stanzas list you can replace the case statement's wild (*) case with a string match for it and code in_stanza_flag=$false to avoid all the remaining guff ending up in the last array element.
catkin:
Uff. This looks complicated. I hoped in some simple solution on one or two lines by awk. I think awk is much more better because it uses strong regular expressions. But thanks for your efforts.
Setting the record separator to blank allows you to look through the file and keep all consecutive lines that contain
something together, so now in your normal grub file a $0 may look like:
@OP - The solution I presented was able to get the necessary lines, but I would hazard a guess that no amount of playing with IFS will work
as it does not take a regular expression as an option (that I know of).
Basically you can't have your cake and eat it too, either you use something like catkin had or if using something like the awk you will need to change
how you deal with the data, ie into an array may not be the best option.
You could perhaps also try converting the awk to output each field using a different separator, maybe pipe, and then use your IFS='\n',
but of course then you need a way to break it back up at the other end.
Maybe if you would enlighten us a little further as to what you need to do with each stanza from menu.lst?
I am going to write a script where I can do this actions:
1) change title
2) change hdx to uuid or uuid to hdx
3) increase hdx+1 or descrease hdx-1
4) save
Setting the record separator to blank allows you to look through the file and keep all consecutive lines that contain
something together, so now in your normal grub file a $0 may look like:
@webhope: if you want each element of the array contain multiple lines, you cannot use "\n" as IFS. Following grail's suggestion you can choose a character that does not appear in menu.lst stanzas, e.g. @, then simply:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.