To create Automation - powershell - powershell-4.0

I am trying to do automation where the scenario is like: I am having a text file ABC.txt which contains all numbers :
ABC.txt:
12234
12345
123455
Through the PowerShell scripting, I want to have the output in a text file like :
Sunny day
Rainy Day
Windy day
This is 12234
This is 12345
This is 123455
So I have performed the below scripting:
$File1 = #((Get-Content "E:\ABC.txt"))
For($a = 0;$a -lt $File1.count;$a++){-join ("This is ", $File1[$a]) | Out-File -FilePath "E:\XYZ.txt" -Append}
The above script is showing output in XYZ.txt file:-
This is 12234
This is 12345
This is 123455
Can you please let me how to add the following on top of the above output:
Sunny day
Rainy Day
Windy day

Related

Bash Scripting - Variable Concatenation

Completely new to Linux and Bash scripting and I've been experimenting with the following script :
declare -a names=("Liam" "Noah" "Oliver" "William" "Elijah")
declare -a surnames=("Smith" "Johnson" "Williams" "Brown" "Jones")
declare -a countries=()
readarray countries < $2
i=5
id=1
while [ $i -gt 0 ]
do
i=$(($i - 1))
rname=${names[$RANDOM % ${#names[#]}]}
rsurname=${surnames[$RANDOM % ${#surnames[#]}]}
rcountry=${countries[$RANDOM % ${#countries[#]}]}
rage=$(($RANDOM % 5))
record="$id $rname $rsurname $rcountry"
#record="$id $rname $rsurname $rcountry $rage"
echo $record
id=$(($id + 1))
done
The script above produces the following result :
1 Liam Williams Andorra
2 Oliver Jones Andorra
3 Noah Brown Algeria
4 Liam Williams Albania
5 Oliver Williams Albania
but the problem becomes apparent when the line record="$id $rname $rsurname $rcountry" gets commented and the line record="$id $rname $rsurname $rcountry $rage" is active where the exact output on the second execution is :
4William Johnson Albania
2Elijah Smith Albania
2Oliver Brown Argentina
0William Williams Argentina
3Oliver Brown Angola
The file I am reading the countries from looks like this :
Albania
Algeria
Andorra
Angola
Argentina
Could you provide an explanation to why this happens?
Your countries input file has DOS-style <cr><lf> (carriage-return line-feed) line endings.
When you read lines from the file, each element of the countries array ends up looking like somename<cr>, and when printed the <cr> moves the cursor back to the beginning of the line, so the contents of $rage end up overwriting the beginning of the line.
The fix is to convert your countries input to use Unix style (<lf> only) line endings. You can do this with dos2unix <inputfile> > <outputfile>, for example.

How to reduce run time of shell script? [duplicate]

This question already has answers here:
Take nth column in a text file
(6 answers)
Closed 2 years ago.
I have written a simple code that takes data from a text file( which has space-separated columns and 1.5 million rows) gives the output file with the specified column. But this code takes more than an hr to execute. Can anyone help me out to optimize runtime
a=0
cat 1c_input.txt/$1 | while read p
do
IFS=" "
for i in $p
do
a=`expr $a + 1`
if [ $a -eq $2 ]
then
echo "$i"
fi
done
a=0
done >> ./1.c.$2.column.freq
some lines of sample input:
1 ib Jim 34
1 cr JoHn 24
1 ut MaRY 46
2 ti Jim 41
2 ye john 6
2 wf JoHn 22
3 ye jOE 42
3 hx jiM 21
some lines of sample output if the second argument entered is 3:
Jim
JoHn
MaRY
Jim
john
JoHn
jOE
jiM
I guess you are trying to print just 1 column, then do something like
#! /bin/bash
awk -v c="$2" '{print $c}' 1c_input.txt/$1 >> ./1.c.$2.column.freq
If you just want something faster, use a utility like cut. So to
extract the third field from a single space delimited file bigfile
do:
cut -d ' ' -f 3 bigfile
To optimize the shell code in the question, using only builtin shell
commands, do something like:
while read a b c d; echo "$c"; done < bigfile
...if the field to be printed is a command line parameter, there are
several shell command methods, but they're all based on that line.

Unix shell - search Tab delimted file

Hi I have a tab delimited text file. The columns are age and name.
Number of rows are 50.
I want to find and output to a new file, the rows, when they age is "eighteen"
cat old_filename | grep "eighteen" > new_filename
when I try , it does not output the rows into the newfile but only the first instance it finds eighteen
grep can check inside files, it matches lines by default.
So if the input was:
name age
tom eighteen
joe sixteen
sam eighteen
grep "eighteen" old_filename > new_filename
new_filename would have
tom eighteen
sam eighteen

Extract text and evaluate in bash

I need some help getting a script up and running. Basically I have some data that comes from a command output and want to select some of it and evaluate
Example data is
JSnow <jsnow#email.com> John Snow spotted 30/1/2015
BBaggins <bbaggins#email.com> Bilbo Baggins spotted 20/03/2015
Batman <batman#email.com> Batman spotted 09/09/2015
So far I have something along the lines of
# Define date to check
check=$(date -d "-90 days" "+%Y/%m/%d")
# Return user name
for user in $(command | awk '{print $1}')
do
# Return last logon date
$lastdate=(command | awk '{for(i=1;i<=NF;i++) if ($i==spotted) $(i+1)}')
# Evaluation date again current -90days
if $lastdate < $check; then
printf "$user not logged on for ages"
fi
done
I have a couple of problems, not least the fact that whilst I can get information from places I don't know how to go about getting it all together!! I'm also guessing my date evaluation will be more complicated but at this point that's another problem and just there to give a better idea of my intentions. If anyone can explain the logical steps needed to achieve my goal as well as propose a solution that would be great. Thanks
Every time you write a loop in shell just to manipulate text you have the wrong approach (see, for example, https://unix.stackexchange.com/questions/169716/why-is-using-a-shell-loop-to-process-text-considered-bad-practice). The general purpose text manipulation tool that comes on every UNIX installation is awk. This uses GNU awk for time functions:
$ cat tst.awk
BEGIN { check = systime() - (90 * 24 * 60 * 60) }
{
user = $1
date = gensub(/([0-9]+)\/([0-9]+)\/([0-9]+)/,"\\3 \\2 \\1 0 0 0",1,$NF)
secs = mktime(date)
if (secs < check) {
printf "%s not logged in for ages\n", user
}
}
$ cat file
JSnow <jsnow#email.com> John Snow spotted 30/1/2015
BBaggins <bbaggins#email.com> Bilbo Baggins spotted 20/03/2015
Batman <batman#email.com> Batman spotted 09/09/2015
$ cat file | awk -f tst.awk
JSnow not logged in for ages
BBaggins not logged in for ages
Batman not logged in for ages
Replace cat file with command.

Bash shell scripting - Error setting variables

I'm new at bash scripting. I tried the following:
filename01 = ''
if [ $# -eq 0 ]
then
filename01 = 'newList01.txt'
else
filename01 = $1
fi
I get the following error:
./smallScript02.sh: line 9: filename01: command not found
./smallScript02.sh: line 13: filename01: command not found
I imagine that I am not treating the variables correctly, but I don't know how. Also, I am trying to use grep to extract the second and third words from a text file. The file looks like:
1966 Bart Starr QB Green Bay Packers
1967 Johnny Unitas QB Baltimore Colts
1968 Earl Morrall QB Baltimore Colts
1969 Roman Gabriel QB Los Angeles Rams
1970 John Brodie QB San Francisco 49ers
1971 Alan Page DT Minnesota Vikings
1972 Larry Brown RB Washington Redskins
Any help would be appreciated
When you assign variables in bash, there should be no spaces on either side of the = sign.
# good
filename0="newList01.txt"
# bad
filename0 = "newlist01.txt"
For your second problem, use awk not grep. The following will extract the second and third items from each line of a file whose name is stored in $filename0:
< $filename0 awk '{print $2 $3}'
In bash (and other bourne-type shells), you can use a default value if a variable is empty or not set:
filename01=${1:-newList01.txt}
I'd recommend spending some time with the bash manual: http://www.gnu.org/software/bash/manual/bashref.html
Here's a way to extract the name:
while read first second third rest; do
echo $second $third
done < "$filename01"

Resources