I have 2 txt file that's have data like that :
Account.txt
12345
12346
12347
Card.txt
1111
2222
3333
i want to loop both txt file in sametime and input into Text box Card and textbox account .
<form action="">
Account:<br>
<input type="text" name="acc">
<br>
Card:<br>
<input type="text" name="card">
</form>
<button type="submit" form="form1" value="Submit">Bind</button>
I already try to loop but i can only loop one txt file . i can't loop 2 txt file in same time to input textbox .
Can anyone give me any suggestion ?
You can use a For-in-zip loop
For example, assuming you read both files and split the data so that you have two arrays #{account} and `#{card}, you can iterate over both lists at the same time like this:
:FOR ${account} ${card} IN ZIP ${account} ${card}
\ log account: ${account} card: ${card}
Here's a complete example:
*** Test Cases ***
Example
#{account}= Create List 12345 12346 12347
#{card}= Create List 1111 2222 3333
:FOR ${account} ${card} IN ZIP ${account} ${card}
\ log account: ${account} card: ${card}
Just run an independent FOR-loop und use a variable as index for your arrays:
#{account} Create List 12345 12346 12347
#{card} Create List 1111 2222 3333
:FOR ${iter} IN RANGE 3
Log Account: #{account}[${iter}]
Log Card: #{card}[${iter}]
Output:
INFO : #{account} = [ 12345 | 12346 | 12347 ]
INFO : #{card} = [ 1111 | 2222 | 3333 ]
INFO : Account: 12345
INFO : Card: 1111
INFO : Account: 12346
INFO : Card: 2222
INFO : Account: 12347
INFO : Card: 3333
Related
I did and try but not able to rectify
opal#opal-Inspiron-15-3567:~/PRABHAT/unix$ bash valcode.sh
valcode.sh: line 5: unexpected EOF while looking for matching ``' valcode.sh: line 19: syntax error: unexpected end of file
IFS="|"
while echo "Enter deparment code:" ; do
read dcode
set -- `grep "^$dcode" <<-limit
01|accounts|6123
02 | admin | 5423
03 | marketing |6521
04 | personnel |2365
05 | production | 9876
06 | sales | 1006
limit'
case $# in
3) echo "deparment name : $2\nEmp-id of head of dept :$3\n"
shift 3 ;;
*) echo "Invalid code" ; continue
esac
done
the output is not coming as per desire
On line 4 you write `grep but the backtick ` is unmatched. Backticks always come in pairs so the interpreter keeps going looking for the match. Eventually it reached the end of the file without finding it and gives up.
Adding the matching backtick (at the end of the line?) will solve this problem.
The snippet reads as below:
echo -n "Filename: "
read filename
echo -n "Data Fields? "
read -a ar
awk -F '[[:space:]][[:space:]]+' 'BEGIN{OFS = "--"} {for val in "${ar[#]}" printf $val }' $filename
ar is an array i am reading, filename is name of a file i am reading too.
The file looks as below :
100 Thomas Manager Sales $5,000
200 Jason Developer Technology $5,500
300 Sanjay Sysadmin Technology $7,000
400 Nisha Manager Marketing $9,500
500 Randy DBA Technology $6,000
What i am trying to do is, accept the filename from user, also take the field number he wants to display, and then scan the file and produce the output accordingly.
The array "ar" contains the field numbers, like 1,2,3.
Input is as below:
$ sh awk_prac.sh
Filename: employee.txt
Data Fields: 2 3
Now according to the input i gave above, the output should look like below:
Thomas Manager
Jason Developer
Sanjay Sysadmin
Nisha Manager
Randy DBA
But everytime i run the code, it shows a syntax error pointing under "val" after for.
awk: cmd. line:1: BEGIN{OFS = "--"} {for val in "${ar[#]}" printf $val }
awk: cmd. line:1: ^ syntax error
Can anyone point out the mistake. #i_am_new_to_this#
The problem with your code is explained in the comments under your question (awk is not shell), here's how to solve it:
$ cat tst.sh
echo -n "Filename: "
read filename
echo -n "Data Fields? "
read fields
awk -v fields="$fields" 'BEGIN{n=split(fields,f)} {for (i=1; i<=n; i++) printf "%s%s", $(f[i]), (i<n?OFS:ORS)}' "$filename"
$ ./tst.sh
Filename: file
Data Fields? 2 3
Thomas Manager
Jason Developer
Sanjay Sysadmin
Nisha Manager
Randy DBA
sample entry:
post_content: " some <strong >blablablabla</strong> text in <html>"
post_title: Kontakt
post_password:
post_name: kontakt
question:
i have an yaml-file with entries like above and i like to parse the content of post_content with cat & grep and pipe it in to different file.
$ cat posts.yaml | grep post_content >> different-file.yaml
This works. very well :) but in this way i only excude all post_content from the *posts.yaml
on top of it i like to separate each post_content in to separate filea named like post_name.yaml - i think its possible to do with some sed-foo merge this in one line of shell comand. but atm i have no idea to do so.
Try:
awk '/post_content:/{content=$0} /post_name:/{print content>$2".yaml"; close($2".yaml")}' posts.yaml
Example
Consider this test file:
$ cat posts.yaml
post_content: " some <strong >blablablabla</strong> text in <html>"
post_title: Kontakt
post_password:
post_name: kontakt
post_content: " some other text in <html>"
post_title: Kontakt
post_password:
post_name: contact
We then run:
awk '/post_content:/{content=$0} /post_name:/{print content>$2".yaml"; close($2".yaml")}' posts.yaml
After this command is run, there will, in addition to posts.yaml, be two new files in the current directory:
$ ls
contact.yaml kontakt.yaml posts.yaml
The contents of the new files are:
$ cat kontakt.yaml
post_content: " some <strong >blablablabla</strong> text in <html>"
$ cat contact.yaml
post_content: " some other text in <html>"
How it works
/post_content:/{content=$0}
Every time that we reach a line that contains post_content:, we save the line in variable content.
/post_name:/{print content>$2".yaml"; close($2".yaml")}
Every time that we reach a line that contains post_name:, we print the variable content to a file whose name is given by the second field on the line followed by.yaml`.
I use ack very often, but something I have not been able to discover is how to make it add new lines when using the output parameter.
Example
echo 'name=Joe,id=123' | ack '^name=(\w+),id=(\d+)' --output="User name: $1\nUser ID: $2"
I expect
User name: Joe
User ID: 123
but I get
User name: Joe\nUser ID: 123
Anyway to make ack respect the new lines characters in the output?
Thanks
You are passing \n to ack instead of a newline. Replace
--output='User name: $1\nUser ID: $2'
with
--output='User name: $1
User ID: $2'
or
--output="$( printf 'User name: $1\nUser ID: $2' )"
or
--output=$'User name: $1\nUser ID: $2' # bash
You can put arbitrary code into that string via #{[ … ]} interpolation. For some reason a literal "\n" string won't work, but chr 0x0A or $/ does.
Example:
$ echo a b | ack '(\w+)\s+(\w+)' --output '$1#{[ chr 0x0A ]}$2'
Output:
a
b
Note that this kind of functionality is likely to break in the future, see also discussions like https://github.com/beyondgrep/ack2/issues/531
I grep a pattern from a directory and the 4 lines before that pattern, I need to further grep the top line from each result , but not getting how to do .
Please suggest regarding this.
The problem explained with example :
in a directory 'direktory'
there are multiple files with different name like 20130611 and 2013400 etc..
the data wrote in the files, which I am interested in is like this :
[
My name is
.....
......
......
Name has been written above
]
now in every instance "Name has been written above" is written in the unit of lines but the value keep on changing in place of "My name is" so I want to grep this particular line from every occurrence .
Please suggest some method to get the result.
Thanks in advance.
a#x:/tmp$ cat namefile
[
My name is
.....
......
......
Name has been written above
]
a#x:/tmp$ cat namefile | grep -B 4 "Name has been written above" | head -1
My name is
Where "4" can be replaced by N i.e. number of lines the target data lies above the grepped line
Try something like
for file in $(ls <wherever>)
do
# Tell the user which file we're looking at
echo ""
echo $file
echo ""
# Output the first line of the file
head -1 $file
# Output the line continaing <pattern> and the four
# preceding lines
<your grep command here>
done