Skip to next line if the variable is string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Skip to next line if the variable is string
# 1  
Old 05-25-2016
Skip to next line if the variable is string

Hi

I have the follwoing requirement
I have a file as follows:
Code:
# cat priy
yyy.poweroff_cmd = /sbin/poweroff
hhh.powersave-nap = 1

When this file is provided as input, I first used "awk" command and saved variables present after "="
Code:
replace=$line
replace1=`echo $line | awk -F "=" '{print $1}'`

Now i see the output of replace1 at first iteration as "/sbin/poweroff"
My problem is if i see the output of replace1 as a string I should skip this line and proceed to second iteration where I can fetch the second line
Please help..Smilie
# 2  
Old 05-25-2016
No need to bother awk unless you are using a very old shell. With e.g. a recent bash, you could try
Code:
replace="yyy.poweroff_cmd = 1"
[[ "${replace##* }" =~ [^0-9] ]] && echo string || echo number
number
replace="yyy.poweroff_cmd = /sbin/poweroff"
[[ "${replace##* }" =~ [^0-9] ]] && echo string || echo number
string

If you do the extra mile and make the config file's entries likehhh.powersave-nap=1, you could source them and immediately have the variables defined for later processing.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 05-25-2016
One way is to assume the number is not zero - using awk (use this code for the two lines of shell you show)

Code:
replace1=$(awk ' int ($(NF) ) == 0 {continue }  # int returns zero when $NF is a string 
                         {print $(NF) }' priy )

This logic may not work for what you are doing.
This User Gave Thanks to jim mcnamara For This Post:
# 4  
Old 05-26-2016
It worked .. Thanks Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need help with how to search a file for a variable string and delete that line

Hi, I have a working script. It does what I am intending it to but a bit confused whether the sed part is supposed to be working or not. Further down is the script with the sed part that should have been working but not and the grep -v part which is the workaround that I am using at the... (10 Replies)
Discussion started by: newbie_01
10 Replies

2. Shell Programming and Scripting

awk to skip header row and add string to field

The awk below does put in VUS in the 9th field but I can not seem to skip the header then add the VUS. I tried to incorporate NR >=2 and NR > 1 with no luck. Thank you :). input Chr Start End Ref Alt Func.refGene PopFreqMax CLINSIG Classification chr1 43395635 ... (5 Replies)
Discussion started by: cmccabe
5 Replies

3. Shell Programming and Scripting

Deleting double quoted string from a line when line number is variable

I need to remove double quoted strings from specific lines in a file. The specific line numbers are a variable. For example, line 5 of the file contains A B C "string" I want to remove "string". The following sed command works: sed '5 s/\"*\"//' $file If there are multiple... (2 Replies)
Discussion started by: rennatsb
2 Replies

4. Shell Programming and Scripting

Skip first and last line

Hi All I have a sample file like below: 012312112 1372422843 1236712 1372422843 1275127 3109301010 from which I wan't to: 1.)delete... (10 Replies)
Discussion started by: swasid
10 Replies

5. Shell Programming and Scripting

Replace all but skip first instance in a line

I have a record like the one given below. 010000306551~IN ~N~ |WINDWARD PK|Alpharetta| If ~ is present more than instance in a line,then I need to delete those instances. Any ideas? I am working in Solaris (7 Replies)
Discussion started by: prasperl
7 Replies

6. Shell Programming and Scripting

Read Line and sent as variable for each string

Hi , I want to read below output, lets called it output1.txt and each string for every line will be declare as a variables. This is the input file 196 server_a server_unix_2 FW 196 server_b server_win_1 CD 196 server_c server_win_2 CD 196 server_bd ... (2 Replies)
Discussion started by: sQew
2 Replies

7. Shell Programming and Scripting

Cut string from a line into a variable

Hi, I am working on a ksh script and I´m stuck on the following: I have to get the pthread_id from a procstack file for a particular tid#. ---------- tid# 1274057 (pthread ID: 1800) ---------- ---------- tid# 1736913 (pthread ID: 4019) ---------- ---------- tid# 1478705 (pthread ID: ... (7 Replies)
Discussion started by: tmf33uk
7 Replies

8. Shell Programming and Scripting

search a string in a line and save it in a variable

Hi I want to read a file line by line and search for a particular string in each line(say for example string containing @ )and save that string into a variable. Can someone suggest me the way to implement it.I am using K- shell Thanks Ishita (5 Replies)
Discussion started by: Ishita
5 Replies

9. Shell Programming and Scripting

how to get the string stored in a variable in a line???

Hi all, I want to search for a data type in a line.For this in a loop i am checking for $DATA_TYPE in a line using grep.But grep is not able to find when i give this. Can any one tell me how to check string in $DATA_TYPE variable in line usign grep (or) any other way to do the above task. ... (4 Replies)
Discussion started by: jisha
4 Replies

10. Shell Programming and Scripting

Skip new line

Hi, how can I skip the new line of echo? In SH!!!! echo "the date is :" date and result I want is the date is : Tue Oct 11 22:24:37 WEST 2005 I've already tried including the \c inside the echo, but it didn't work. Thanks! (2 Replies)
Discussion started by: pmpx
2 Replies
Login or Register to Ask a Question