Calculate the length of the each variable and matches to the desired value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calculate the length of the each variable and matches to the desired value
# 1  
Old 10-11-2011
Calculate the length of the each variable and matches to the desired value

My script is currently doing two function and I want to add one more as pre the question.

Code:
 
echo -en "Enter the logmsg="
read logmsg
logmsg1=${logmsg%%|*};
logmsg_len2=$(echo ${logmsg} | awk '{print $2}');
logmsg_suffix="|";
Char()
{
#echo $logmsg1
echo $logmsg1 | tr ',' '\n' | awk '{if($0~/^D/||/^S/) {print $0" starts"} else {print " Please check your input(e.g. Start it with D or S)";exit;}}'
}
Pipe()
{
if [[  "${logmsg_len2}" != "${logmsg_suffix}" ]]; then
                 echo -e "error use | symbol"
                 exit 1;
     else
   echo "success"
 fi
}
Char
Pipe





This is the complete string which I am using in the script to get the desired results

D0000,D12345,S1234,D12345 | KestrelPrdV2_0.build.177 - svn 141115 tag as V2.0.0.14

Now my script will cut the left part (left from | symbol)
D0000,D12345,S1234,D12345

Now my question is how to calculate the length of the each varibale and also it should not more than 6 i.e. -le 6.Number of variables can be increases or decreases upto infinity.

Please let me know if more information require.
# 2  
Old 10-11-2011
Code:
 echo 'D0000,D12345,S1234,D12345 | KestrelPrdV2_0.build.177 - svn 141115 tag as V2.0.0.14' | nawk -F'[,|]' '{for(i=1;i<NF;i++) print $i "->" length($i)}'

# 3  
Old 10-12-2011
Hi ,

Its working fine but here I want one more thing that if length is above 6 than it should be exited otherwise successful.

Thanks in advance

---------- Post updated at 02:08 AM ---------- Previous update was at 01:17 AM ----------

one more thing I can see here that is

echo "D0000,D12345,S1234,D12345 | KestrelPrdV2_0.build.177 - svn 141115 tag as V2.0.0.14" | awk -F'[,|]' '{for(i=1;i<NF;i++) print $i "->" length($i)}';
D0000->5
D12345->6
S1234->5
D12345 ->7

It gives the wrong length of the last variable.

Kindly advise
# 4  
Old 10-12-2011
code is perfect ..
One extra space character is present after "5" in D12345 .. So the length is "7"
# 5  
Old 10-12-2011
Code is perfect but the conditon was that there should be space left and right of the pipe symbol...and one more thing that is if length is above 7 it should be exited.


Please advise
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert variable length record to fixed length

Hi Team, I have an issue to split the file which is having special chracter(German Char) using awk command. I have a different length records in a file. I am separating the files based on the length using awk command. The command is working fine if the record is not having any... (7 Replies)
Discussion started by: Anthuvan
7 Replies

2. Shell Programming and Scripting

changing a variable length text to a fixed length

Hi, Can anyone help with a effective solution ? I need to change a variable length text field (between 1 - 18 characters) to a fixed length text of 18 characters with the unused portion, at the end, filled with spaces. The text field is actually field 10 of a .csv file however I could cut... (7 Replies)
Discussion started by: dc18
7 Replies

3. Shell Programming and Scripting

Make variable length record a fixed length

Very, very new to unix scripting and have a unique situation. I have a file of records that contain 3 records types: (H)eader Records (D)etail Records (T)railer Records The Detail records are 82 bytes in length which is perfect. The Header and Trailer records sometimes are 82 bytes in... (3 Replies)
Discussion started by: jclanc8
3 Replies

4. Shell Programming and Scripting

How can use the perl or other command line to calculate the length of the character?

For example, if I have the file whose content are: >HWI-EAS382_30FC7AAXX:7:1:927:1368 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA >HWI-EAS382_30FC7AAXX:7:1:924:1373 ACGAACTTTAAAGCACCTCTTGGCTCGTATGCCGTC I want my output calculate the total length of nucleotide. So my output should look like this:... (1 Reply)
Discussion started by: patrick chia
1 Replies

5. Solaris

Calculate the length and add some string infront

Hi all, I have file having 4 coulmn like 9246309198,406655682,400009246309198 9246309198,9246309198,400009246309198 9246309198,9246309198,400009246309198 9246309198,9246309198,400009246309198 9246309198,406655682,400009246309198 9246309198,9246309198,400009246309198 I want to valid... (4 Replies)
Discussion started by: salaathi
4 Replies

6. Shell Programming and Scripting

creating a fixed length output from a variable length input

Is there a command that sets a variable length? I have a input of a variable length field but my output for that field needs to be set to 32 char. Is there such a command? I am on a sun box running ksh Thanks (2 Replies)
Discussion started by: r1500
2 Replies
Login or Register to Ask a Question