Sed- Replace space in filename by a \


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sed- Replace space in filename by a \
# 1  
Old 08-07-2013
Sed- Replace space in filename by a \

Code:
`echo $file | sed 's/ / /g'`

Hey guys I want help in converting the spaces in my file names to '\ ' .

Example:
UK maps --> UK\ maps

Could someone please help me.

I have tried the following sequences already (none of them work):
Code:
1)s/ /\ /g
2)s/ /\\ /g
3)s/ /\\\ /g

Can someone please help me

Thanking you
Neil

Last edited by Scott; 08-07-2013 at 06:55 AM.. Reason: Code tags
# 2  
Old 08-07-2013
Hello,

Please use code tags if you are giving the commands in the post.
Could you please use the following command and let me know if it helps.

Lets say we have a variable named a that contains the value in which we want to remove the space.

Code:
$ a="UK maps"

Here is the command to remove spaces.

Code:
$ echo $a | sed 's/ /g/'


Output should be as follows.

Code:
UKgmaps
$


Please let me know if this helps.


Note: Similarly we can use the with a file too.



Thanks,
R. Singh
# 3  
Old 08-07-2013
sed command

Code:
for file in `find . -type f -print | cut -c 3- | sed 's/ /#}/g'`
do
  file2=`echo $file | sed 's/#}/ /g'`
  file3=`echo $file | sed 's/#}//g'`
  size=`du -k $file3`
  result=`grep -c "$file2" $dbdump`
  if [[ $result = 0 ]]; then
    echo $file3>> $notusedfile
  else
    echo $file2 >> $usedfile
fi
done


Hi ,

Actually my code is as given above. I want to calculate the file size of each file.But some files have spaces in them due to which I am unable to use the du -k command.

I am trying to use sed to replaces all ' ' in the filenames with a '\ '.

The command you gave me will remove spaces which will still not give me the file size for file names with spaces in them.

Please help me with this

Thanks,
Neil
# 4  
Old 08-07-2013
Hello Neil,

If I undetstood your requirement correctly you want to get the file names as well as sizes in a specific path. If this is the requirement then please use the following code else get back to me with correct Input and Output.


Command to be used as follows.
Code:
$ ls -ltr | awk '{print$5"  "$9}'


Output will be as follows.

Code:
51  without_length_70_lines
66  value_braces
82  to_find_min_and_max_number
441  timings_file_query.ksh
114  timings_file_query
3164  test_data
233  test_chumma1
1287  test5
303  test4
98  test13
287  test1212
49  test1211
150  test121
66  test11
92  test1
155  test.ksh
85  string_occurance
339  script_user_error.ksh
846  reverse_test_data
168  replacetext.ksh
861  remove_dulpicate1
240  process_testing.ksh
0  output1
1  outpout
25  myfile2.txt
21  myfile1.txt
1  myfile.txt
122  move_latest_file.ksh
6394  mail_body
30  ls_checking1.sh
29  ls_checking.ksh
181  loop_example.ksh
192  length_testing.ksh
25  join_example2
6  join_example1
248  input_win_count_file
396  input1
432  grep_string
160  grep_starting
396  great_command_input
4620  formats_rough_info
836  formats_actual_info
512  files_example
477  file_new_output
11  file_names


Note: It is just an example like what ever files I have in a directory.


Thanks,
R. Singh
# 5  
Old 08-07-2013
Hi Ravindra,

Actually no. All I need is this:
Code:
# start a loop where all files are considered one by one do
for file in `find . -type f -print | cut -c 3- }/g'`
  # get the file name from this instance and replace space by '\ '
  file2=`echo $file | sed 's/ /\ /g'`

  # then find the size of that file using du -k
  size=`du -k $file3` 
done

I need the code in that order as I do not want to find the size of all files in a directory. Rather I am comparing some properties of a file and if they dont match I need to make a list of the name of that file and its size. The issue is du -k does not give sizes fo filenames with a space in them. So, I need to escape it using a backslash so for example:
Code:
file=/usr/Neil C/o.jpg
file3=/usr/Neil\ C/o.jpg
size=`du -k $file3` #(4 or something)

I cannot use ls -ltr as I dont not want the sizes of all files.

Thanks
Neil

Last edited by Scott; 08-07-2013 at 07:00 AM.. Reason: Use code tags, please...
# 6  
Old 08-07-2013
Hello Neil,

Could you please use the following code.


Code:
 
$ cat file_size_file_name.ksh
values_files=`ls -ltr | grep '^-' | awk '{print$9}'`
set -A array_values_files ${values_files}
for i in ${array_values_files[@]}
do
value_of_file_space=`echo $i | du -k $i`
echo $value_of_file_space
 
 
done
$


Output should be as follows.



Code:
16 testing_appending_file_date
0 test
4 1
4 appending_file.ksh
28 check_file1
4 grep_dat_format
4 check_string
0 test_files
0 test_files.1
0 test_files.2
0 test_files.3.bin.Z
4 test_file121
4 find_replace.ksh
4 check_pattern_file1
4 df_test_removing_column
4 check_file_dctm
4 find_count_digits
4 find_count_digits.ksh
4 file_space
4 remove_duplicate_remove_text
4 file_size_file_name.ksh
$ cat file_size_file_name.ksh


Let me know if this helps.



Thanks,
R. Singh

Last edited by RavinderSingh13; 08-07-2013 at 04:13 AM..
# 7  
Old 08-07-2013
Hi Ravindra,

Thanks a lot but I do not think this helps. I already have a code format in which I need to fit my code. This would be rewriting the entire code which I cannot do.

Is it not possible the way I have requested for it? If its not then please let me know.

Regards,
Neil
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace space with !@ using sed

Hello I have a requirement where I need to replace space :61 with !@ :61 Source :60F:123 :61:151 :61:151 :61:15101 Target :60F:123 :61:151!@:61:151!@:61:15101 I cant use and command as I don't want the tab to be converted . commands not working : sed 's/... (5 Replies)
Discussion started by: kamijia83
5 Replies

2. Shell Programming and Scripting

sed to replace pattern with filename

Hi all, I'm trying to replace a pattern/string in about 100 files with the filename using following commands but getting nowhere: for f in *.fa; do sed "s/^>.*/>$f/g" $f > $f_v1.fa; done for f in *.fa; do sed 's/^>.*/>`echo $f`/' > $fa_v1.fa; done Basically I want to change any line... (5 Replies)
Discussion started by: ivpz
5 Replies

3. Shell Programming and Scripting

sed the find the third , and replace with a space

Dear All, I am new here. Could anyone help to find a sed script for replace the third "," to " "? input: abc,def,ghi,jkl,mno,pqr,stu,vxz output: abc,def,ghi jkl,mno,pqr,stu,vxz Many thanks. Please use code tags next time for your code and data. (2 Replies)
Discussion started by: mimilaw
2 Replies

4. Shell Programming and Scripting

replace space with the help of sed

Hi, i have below string - mynameis arpit i want output like below - mynameis\ arpit that i am getting from below - temp='mynameis arpit' echo $temp|sed 's//\\ /g' --> mynameis\ arpit now i am doing - (2 Replies)
Discussion started by: thearpit
2 Replies

5. Shell Programming and Scripting

Replace comma with a blank space using SED

Hello everyone, I want to replace all "," (commas) with a blank space My command thus far is: cat test.text | sed -e s/\`//g | awk '{print$1" "$2" "$3}' I'm sure you guys know this, but the SED command that I am using is to get rid of the "`" (tics). which gives me: name ... (5 Replies)
Discussion started by: jayT
5 Replies

6. Shell Programming and Scripting

How to use sed to replace space partial

source "PUT 810 712 0001 ILC AK4 00 0 00 00" It needs to be changed to "PUT,810,712,0001,ILC,AK4 00 0 00 00" Thanks in advance. (6 Replies)
Discussion started by: janex
6 Replies

7. Shell Programming and Scripting

Using sed I want to replace space by newline

Input: -------------------------- 123asd 456sdasda 789a ------------------------- output wanted: --------------------- 123asd 456sdasda 789a ---------------------- I want this by sed in simple way please help (I know by: tr ' ' '\n' < inputfile )I want it by sed only (5 Replies)
Discussion started by: RahulJoshi
5 Replies

8. Shell Programming and Scripting

Replace 2 Occurances of Space (sed)

I have a large file that looks like the below output: system SUNWxwmod X Window System kernel modules system SUNWxwoft X Window System optional fonts system SUNWxwopt X Window System Optional Clients system ... (1 Reply)
Discussion started by: hxman
1 Replies

9. Shell Programming and Scripting

Replace a filename with full path using sed

hi, i need to replace a line a file with a new raw device location.. original file.. /opt/sybase/ASE1502/ASE-15_0/bin/dataserver \ -d/data/TST_AKS1/sybdevices/master.dat \ -e/logs/sybase/TST_AKS1/SFO_TST_AKS1.log \ -c/apps/sybase/ASE1502/ASE-15_0/TST_AKS1.cfg \... (2 Replies)
Discussion started by: aksaravanan
2 Replies

10. Shell Programming and Scripting

need help on sed (replace string without changing filename)

I have awhole bunch of files and I want to edit stringA with stringB without changing the filename. I have tried the following sed commands: sed "s/stringA/stringB/g" * This will print the correct results but does not actually save it with the new content of the file. when I do a cat on... (5 Replies)
Discussion started by: jjoves
5 Replies
Login or Register to Ask a Question