Remove appending 0 from file variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove appending 0 from file variable
# 1  
Old 05-16-2013
RedHat Remove appending 0 from file variable

Dear All,

i have filename RYK3201_032001002.pdf

and i am using below command to get a file

Code:
file_name1=$(echo $file_name | cut -d "_" -f2 | cut -d "." -f1 | cut -c -6)

and then
Code:
file_name2=${NewFile_NAME}_$file_name1

now the value of file_name1 will be 032001

i want to file_name1 should be 32001 means remove all prefix 0 even if there are more than one 0.

here the point should be considered that if there is no prefix 0 it should not replace next zero in string

if somebody have idea please share



---------- Post updated at 09:26 AM ---------- Previous update was at 09:06 AM ----------

I used this but it is just replacing first occurance but not all
Code:
sed 's/^0\(.*\)/\1/'


Last edited by Scrutinizer; 05-16-2013 at 11:28 AM.. Reason: code tags placement + extra code tags
# 2  
Old 05-16-2013
Try:
Code:
sed 's/.*_0*//; s/...\..*$//'

# 3  
Old 05-16-2013
Code:
file_name="RYK3201_032001002.pdf"
file_name=$(awk -F_ '{print substr($2,0,7)+0}' <<<$file_name)
echo $file_name
32001

# 4  
Old 05-16-2013
RedHat

Quote:
Originally Posted by Scrutinizer
Try:
Code:
sed 's/.*_0*//; s/...\..*$//'

thanks but i got below output

Code:
$ echo RYK3201_0003200121.pdf | cut -d "_" -f2 | cut -d "." -f1 | cut -c -6 | sed 's/.*_0*//; s/...\..*$//'
000320

# 5  
Old 05-16-2013
Code:
$ echo RYK3201_0003200121.pdf |sed 's/.*_0*//; s/..\..*$//'
32001

# 6  
Old 05-16-2013
With a Korn shell with a version later than 1988 and with a current bash, you can also do this just using variable expansions built into the shell:
Code:
file_name=RYK3201_032001002.pdf
NewFile_NAME=NFN
file_name1=${file_name##*_}             # discard RYK3201_
file_name1=${file_name1%.*}             # discard .pdf
file_name1=${file_name1:0:6}            # keep 1st 6 characters
file_name1=${file_name1##0}             # discard leading zeros
file_name2="${NewFile_NAME}_$file_name1"
printf "original: %s, file_name1: %s, file_name2: %s\n" "$file_name" "$file_name1" "$file_name2"

which produces:
Code:
original: RYK3201_032001002.pdf, file_name1: 32001, file_name2: NFN_32001

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Two question: remove from the other variable or file to get another variable or file

Question 1: A="a b c d e f g 1 2 3 4 5" B="c 3" get C="a b d e f g 1 2 4 5" Question 2: cat file1 a b c (6 Replies)
Discussion started by: yanglei_fage
6 Replies

2. Shell Programming and Scripting

Read value of a file, remove first 2 chars and put this value in a variable

Hi i have need of read a file value with cat command and remove first 2character for example cat /sys/class/rtc/day 0x12 Remove char 12 And put this value in a variable is possible with a script thanks for help (6 Replies)
Discussion started by: enaud
6 Replies

3. Shell Programming and Scripting

Appending value to variable

I have a Query! by using command cat >> file1 we can append data's to a already existing file, Similarly is it possible to append a data to a variable. Thanks in Advance!! (2 Replies)
Discussion started by: gwgreen1
2 Replies

4. Shell Programming and Scripting

Appending string, variable to file at the start and string at end

Hi , I have below file with 13 columns. I need 2-13 columns seperated by comma and I want to append each row with a string "INSERT INTO xxx" in the begining as 1st column and then a variable "$node" and then $2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13 and at the end another string " ; COMMIT;" ... (4 Replies)
Discussion started by: Vaddadi
4 Replies

5. Shell Programming and Scripting

To remove the newline character while appending into a file

Hi All, We append the output of a file's size in a file. But a newline character is appended after the variable. Pls help how to clear this. filesize=`ls -l test.txt | awk `{print $5}'` echo File size of test.txt is $filesize bytes >> logfile.txt The output we got is, File size of... (4 Replies)
Discussion started by: amio
4 Replies

6. Shell Programming and Scripting

Appending data into a variable

Hi, I would like to know if it's possible to append data into a variable, rather than into a file. Although I can write information into a temporary file in /tmp, I'd rather if possible write into a variable, as I don't like the idea that should my script fail, I'll be polluting the server with... (5 Replies)
Discussion started by: michaeltravisuk
5 Replies

7. Shell Programming and Scripting

appending strings to variable

is it possible? as i keep reading a file, i want one particular variable to keep storing the line that i've read so far (1 Reply)
Discussion started by: finalight
1 Replies

8. Shell Programming and Scripting

appending space to variable

Hi I need to write a script where there the user enters 3 input parameter variable number the program should ask the user left or right if it is left , the number specified that many spaces should be added to the value in front of the value and saved in the samee variable itself and if it is... (5 Replies)
Discussion started by: viv1
5 Replies

9. Shell Programming and Scripting

Appending to a variable?

Hey, I'm creating a custom useradd script, and I'm giving the option to add secondary groups. Basically what I want to do is ask for the name of the group, you type in the group you want to add, it assigns that group name to the variable $sgroup. Then the scripts asks if you want add another. If... (0 Replies)
Discussion started by: paqman
0 Replies

10. Shell Programming and Scripting

appending spaces to a variable

Hi All, I have a requirement, in which i have to append some spaces to the variable, and then send it to another function. I am new to the UNIX shell programming. Ultimately the length of the string should be 40 characters. exp: Login = "rallapalli" (length = 10) i have to append 30 spaces to... (2 Replies)
Discussion started by: rallapalli
2 Replies
Login or Register to Ask a Question