split words in MIPS - mips

I want to separate the words in a sentence and I want to pass each of these words to the bottom line in MIPS like this my string: Hello my name is Jack, output should be like this
Output:
Hello
my
name
is
Jack
Can anyone help me ?
I started like this;
.data
string1: .space 100
newLine: .asciiz "\n"
ask: .asciiz "Enter string: "
newstring: .asciiz "New string: "
.text
main:

Related

Tcl: Parsing input with strings in quotes

I have the following code to split stdin into a list of strings:
set cmd [string toupper [gets stdin]]
set items [split $cmd " "]
This splits the user input into a list (items) using the space as a delimiter. It works fine for simple input such as:
HELLO 1 2 3
What I get in items:
HELLO
1
2
But how can I get the quoted string in the example below to be become one item in the list (items):
"HELLO THERE" 1 2 3
What I want in items:
HELLO THERE
1
2
How can I do this?
This is where you get into building a more complex parser. The first step towards that is switching to using regular expressions.
regexp -all -inline {"[^\"]*"|[^\"\s]+} $inputData
That will do the right thing... provided the input is well-formed and only uses double quotes for quoting. It also doesn't strip the quotes off the outside of the "words"; you'll want to use string trim $word \" to clean that up.
If this is a command that you are parsing, use a safe interpreter. Then you can allow Tcl syntax to be used without exposing the guts of your code. I'm pretty sure there are answers here on how to do that already.
Because Tcl doesn't have strong types, the simplest way to do this is to just treat your stdin string like a list of strings. No need to use split to convert a string into a list.
set cmd {"HELLO THERE" 1 2 3}
foreach item $cmd {
puts $item
}
--> HELLO THERE
1
2
3
Use string is list to check if your $cmd string can be treated as a list.
if {[string is list $cmd]} {
puts "Can be a list"
} else {
puts "Cannot be a list"
}

How to print multi line text box value without '\n' by tkinter

I want to print textbox value without '\n' from tkinter,
When I print then it show one line and next line valu adding with "\n', actually I fetch the valu from mysql
To remove a \n from the end of a string, use rstrip('\n'):
lines = [
"Hello, world!\n",
"No newline",
"", # empty
"Two\nNewlines\n",
"Intermediate\nNewline",
]
for line in lines:
s = line.rstrip("\n")
print("-" * 20)
print(s)
# gives
"""
--------------------
Hello, world!
--------------------
No newline
--------------------
--------------------
Two
Newlines
--------------------
Intermediate
Newline
"""

transform multiline text into csv with awk sed and grep

I run a shell command that returns a list of repeated values like this (note the indentation):
Name: vm346
cpu 1 (12%) 6150m (76%)
memory 1130Mi (7%) 1130Mi (7%)
Name: vm847
cpu 6 (75%) 30150m (376%)
memory 12980Mi (87%) 12980Mi (87%)
Name: vm848
cpu 3500m (43%) 17150m (214%)
memory 6216Mi (41%) 6216Mi (41%)
I am trying to transform that data like this (in csv):
vm346,1,(12%),6150m,(76%),1130Mi,(7%),1130Mi,(7%)
vm847,6,(75%),30150m,(376%),12980Mi,(87%),12980Mi,(87%)
vm848,3500m,(43%),17150m,(214%),6216Mi,(41%),6216Mi,(41%)
The problem is that any given dataset like the one above is always on more than one line.
when I pipe that into it awk it drives me mad because even if I use:
BEGIN{ FS="\n" }
to try and stitch the data together in one line, it doesn't work. No matter what I do, awk keeps the name value as a separated line above everything else.
I am sorry I haven't much code to share but I have been spinning my wheels with this for a few hours now and I am running out of ideas...
I can solve this in Perl:
perl -ane 'print join ",", #F[1 .. $#F]; print $F[0] eq "memory" ? "\n" : ","'
It should be easy to translate it to awk if you need it.
How does it work?
-a splits each line on whitespace into the #F array
-n reads the input line by line and runs the code specified after -e for each line
We print all the elements but the first one separated by commas (see join)
We then look at the first column, if it's memory, we are at the last line of the block, so we print a newline, otherwise we print a comma
With AWK, one option is to set RS to "Name: ", and ignore the first record with NR > 1, e.g.
awk -v RS="Name: " 'BEGIN{OFS=","} NR > 1 {print $1, $3, $4, $5, $6, $8, $9, $10, $11}' file
#> vm346,1,(12%),6150m,(76%),1130Mi,(7%),1130Mi,(7%)
#> vm847,6,(75%),30150m,(376%),12980Mi,(87%),12980Mi,(87%)
#> vm848,3500m,(43%),17150m,(214%),6216Mi,(41%),6216Mi,(41%)
awk '{$1=""}1' | paste -sd' \n' - | awk '{$1=$1}1' OFS=,
Get rid of the first column. Join every three rows. Same idea with sed:
sed 's/^ *[^ ]* *//' | paste -sd' \n' - | sed 's/ */,/g'
Something else:
awk '
$1=="Name:" {
sep=ors
ors=ORS
} {
for (i=2;i<=NF;++i) {
printf "%s%s",sep,$i
sep=OFS
}
} END {printf "%s",ors}'
Or if you want to print an ORS based on the first field being "memory" (note that this program may end without printing a terminating ORS):
awk '{for (i=2;i<=NF;++i) printf "%s%s",$i,(i==NF && $1=="memory" ? ORS : OFS)}'
something else else:
awk -v OFS=, '
index($0,$1)==1 {
OFS=ors
ors=ORS
} {
$1=""
printf "%s",$0
OFS=ofs
} END {printf "%s",ors} BEGIN {ofs=OFS}'
This might work for you (GNU sed):
sed -nE '/^ +\S+ +/{s///;H;$!d};x;/./s/\s+/,/gp;x;s/^\S+ +//;h' file
In overview the sed program processes indented lines, already gathered lines (except in the case that the current line is the first line of the file) and non-indented lines.
Turn off implicit printing and enable extended regexp's. (-nE).
If the current line is indented, remove the indent, the first field and any following spaces, append the result to the hold space and if it is not the last line, delete it.
Otherwise, check the hold space for gathered lines and if found, replace one or more whitespaces by commas and print the result. Then prep the current line by removing the first field and any following spaces and replace the hold space with the result.
The solution seems logically back-to-front, but programming in this style avoids having to check for end-of-file multiple times and invoking labels and gotos.
N.B. This solution will work for any number of indented lines.
Here is a ruby to do that:
ruby -e '
s=$<.read
s.scan(/^([^ \t]+:)([\s\S]+?)(?=^\1|\z)/m). # parse blocks
map(&:last). # get data part
# parse and join the data fields:
map{|block| block.split(/\n[ \t]+[^ \t]+[ \t]+/)}.
map{|lines| lines.map(&:strip).join(" ").split().join(",")}.
each{|l| puts "#{l}"}
' file
vm346,1,(12%),6150m,(76%),1130Mi,(7%),1130Mi,(7%)
vm847,6,(75%),30150m,(376%),12980Mi,(87%),12980Mi,(87%)
vm848,3500m,(43%),17150m,(214%),6216Mi,(41%),6216Mi,(41%)
The advantage is that this is not dependent on the number of lines or the number of fields. It is parsing data that is in blocks of the form:
START: ([ \t]+[data_with_no_space])*\n
l1 ([ \t]+[data_with_no_space])*\n
...
START:
...
Works this way:
Parse the blocks with THIS REGEX;
Save an array of the data elements;
Join the sub arrays and then split into data fields;
Join(',') to make a csv.

Jq variable add extra \

Hi i have this bash code
#!/bin/bash
textb="\n 1 \n 2 \n 3 \n 4"
jq --arg textb "$textb" '. | {plain_text: ( $textb + desc.envi )'
When i run the comand this giveme the next example
#!/bin/bash
\\n1 \\n2 \\n3 \\n4
Why jq add and extra "\"? What i m going wrong?
I try some like this
textb="\n" 1 "\n" 2 "\n" 3 "\n" 4"
But i have this result
n1 n2 n3 n4
Thx
\n does not mean linefeed/newline in a bash double quoted string. It's just backslash+lowercase n.
If you use linefeeds instead of backslashes and Ns, they will encode the way you want:
textb="
1
2
3
4"
jq -n --arg textb "$textb" '."the string is:" = $textb'
outputs:
{
"the string is:": "\n1\n2\n3\n4"
}
Here are few other equivalent ways of putting literal linefeeds into a bash variable:
textb=$'\n1\n2\n3\n4'
textb=$(printf '\n%s' {1..4})

How to remove quotes in TCL string by using 'string map'

I am really stuck with this problem for some hours now. I am writing the output of my TCL script to a .csv file. Theoretically it should have 130 lines, but it stops afther line 117, where the first times double-quotes appear. So I want to remove the double-quotes using 'string map' but all the ways I tried it outputs still doublequotes. Other caharcters are cleanly maped as I intended.
set lines [string map [list "\;" {} "," {} {"} {} "\'" {} ] $lines]
set lines [string map [list "\;" {} "," {} "\"" {} {'} {} ] $lines]
set lines [string map [list "\;" {} "," {} \" {} \' {} ] $lines]
Has anyone an idea how to correctly remove double-quotes out of a string?
Thanks!
Paul
The string map calls you use will work — all are using correctly-constructed even-length maps — but it is possible that the characters you see in the input string are not standard double quotes (") but rather their more sophisticated cousins (“, ”). There are a few other characters that can look similar too, such as a naked diaeresis (¨).