How to sort on a text block? - sorting

I have a text of 50000 lines
before change
bird={
explain line1
explain line2
...
}
zero={
explain line1
explain line2
...
}
bed={
explain line1
explain line2
...
}
air={
explain line1
explain line2
...
}
.......
that I would like to change into
air={
explain line1
explain line2
...
}
bed={
explain line1
explain line2
...
}
bird={
explain line1
explain line2
...
}
zero={
explain line1
explain line2
...
}
My solution is divided into four steps:
1.:g/{/;/}/s/$/# (# is not included in the file)
2.:g/{/;/}/j!
3.:sort
4.:%S/#/\r/g
is there a better solution? one step?

Vim has no built-in notion of "treat these blocks as single units" when sorting; also folding won't help. The approach you've outlined (first join all lines of each block into a single long line, then sort, then unjoin) is the canonical workaround.
Of course, if you need this more often, you can define a custom :command for it. For an example (that works with folding) see this.

Related

How to read the second line in a document.txt and then make a loop that reads line +1 in this document

My bot reads emails one by one from a document.txt file and after login with this email the bot outputs the comments that I have in another file.
I have reached the point that the bot reads the emails but I want that a specific account makes a specific and not a repeated comment.
So I have in mind the solution of reading a specific line from the comments file.
For example account 1 reads and puts line 1 of the comments file. I want to know how can I read the second line from a comments file.
This is the code part when I read comments one by one but I want to read for example line two or three!
file = 'comments.txt'
File.readlines(file).each do |line|
comment = ["#{line}"]
comment.each { |val|
comment = ["#{val}"]
}
end
File.readlines returns array. So you can do everything you want
lines = []
File.readlines(path_to_file, chomp: true).each.with_index(1) do |line, line_number|
lines << (line_number == 2 ? 'Special line' : line)
end
Try the below.
# set the line number to read
line_number = 2 # <== Reading 2nd line
comment = IO.readlines('comments.txt')[line_number-1]
Your code is overwriting the comment variable in each iteration.
I'd write your code like this:
lines = File.readlines('comments.txt')
lines.each do |line|
# entire line
end
In the loop you can do a lot of things with the single line, unfortunately I don't get 100% what you want to do (one comment vs. multiple, always the same for specific users, etc.) I hope this helps anyway.

In bash, how do a replace a string in a file with a dynamic replacement

Specifically, I have a Markdown document with figures listed as so
```
![Figure XXX](images/figure-of-a-thing.png)
...
![Figure XXX](images/figure-of-another-thing.png)
```
I'd like to end up with the following:
```
![Figure 1](images/figure-of-a-thing.png)
...
![Figure 2](images/figure-of-another-thing.png)
```
I.e., with a monotonically increasing number. It strikes me that there are some sed/awk ninjas out there who can solve this.
I'd say
awk '/^!\[Figure/ { sub(/XXX/, ++n) } 1' filename.md
This will replace the first occurrence of XXX in all lines that begin with ![Figure with a running counter.
If the ![Figure sequences can also appear in the middle of a line and possibly several times in one line (I don't think this is probable, but for the sake of completeness, let's consider the case), you might use
awk 'BEGIN { n = 1 } { while(sub(/!\[Figure XXX/, "![Figure " n)) ++n; } 1' filename.md

Ruby String mismatch in while comparing text from a text file

I am in a problem while reading a text file with readline and trying to compare first line with a string. I want to compare the first line of the text file with a string and then will go for next process. But I can't do that. Here is my code:
doc = File.open("example.txt", "r")
line1 = doc.readline
if line1 == "sukanta"
line2 = doc.readline
line3 = doc.readline
line4 = doc.readline
end
My example.txt file contains:
sukanta
Software engineer
label2
server:107.108.9.190
Please give me solution. While I am trying to get string length with line1.length it's not showing the exact number.
i got the answer. Its silly mistake .. i should use "sukanta\n" to compare
When i am using readline to read each line then i have to set each line in their place sequentially. i cant break the order. Whil i am using loop like
doc = File.open("example.txt", "r")
doc.each_line do |lines|
puts lines
end
getting the whole text as a line. cant separate each line from others. i need to break the order. How to do that?
I suspect you are not taking into account that a line ends with $/ ("\n" on UNIX). So you probably intended
line1 == "sukanta\n"
or
line1.chomp == "sukanta"
and you are not including $/ when you count the length (which is one or two characters less than the correct length depending on the OS).

Can someone explain this ruby code?

From the rails postgresql_adapter.rb. I get what it's trying to do, I just don't get how it happens. It's really to do with the <<-SQL that I'm lost.
exec_query(<<-SQL, 'SCHEMA', binds).rows.first[0].to_i > 0
SELECT COUNT(*)
FROM pg_tables
WHERE tablename = $1
#{schema ? "AND schemaname = $2" : ''}
SQL
I've seen code before where you could say:
blah = <<-X
some
lines
of
test
X
But I've never seen this done within the argument to a function call. I'm really confused by this. Can someone explain to me what exactly is going on here?
You can use a heredoc-marker (like <<-SQL in your example) anywhere (or even multiple times) in a line and the heredoc will then start on the following line and continue until the end-marker is met (in case of multiple markers, the (n+1)th heredoc will start after the nth end-marker and continue up to the (n+1)th end-marker). The content of each heredoc will then be inserted at the place where the corresponding marker was used.
So
foo(<<BAR, 42)
bar
BAR
is the same as
foo("bar\n", 42)
and
foo(<<BAR, <<BAZ)
bar
BAR
baz
BAZ
is the same as
foo("bar\n", "baz\n")

Splitting a string into multiple strings after a specified character in Ruby

I am trying to create a script that takes in a string with an unknown number of parts that are separated by ';'.
"blah blah one; blah blah two"
I tried to use the (?: re) example from rubydocs but had no luck with it. Can someone show me the best way to do this?
you can just use the split method
a='blah blah blah;blah foo blah'.split(";")
the parts will be store in index from 0 to 1 (in the above example )

Resources