Sponsored Content
Top Forums Shell Programming and Scripting Script to remove spaces and mv a file Post 303016146 by w_s_s on Friday 20th of April 2018 11:25:32 AM
Old 04-20-2018
Script to remove spaces and mv a file

I've tried various solutions to move a file name with spaces and nothing seems to work. I need to take a date as input, prepend it to a filename with spaces then remove the spaces and mv the file to the new name.
Code:
#!/bin/ksh
#

if (( $# != 1 ))
then
   echo "Usage: `basename $0` <DATE> "
   echo ""
   exit 1
fi

echo " "
IN_DATE=${1}
IN_FILE="_test with space.txt"
echo "date ${IN_DATE}"

echo " "
oldFILE="$IN_DATE""$IN_FILE"

echo " "
newFILE=$(echo "$oldFILE" | sed 's/ /_/g')
echo "newFILE ${newFILE}"

echo " "
mv "${oldFile}" "${newFILE}"

which produces...
Code:
space_test 20180419

date 20180419

oldfile 20180419_test with space.txt

newFILE 20180419_test_with_space.txt

mv: cannot stat ‘’: No such file or directory

I've also tried using awk to add single or double quotes around the original file name.
Code:
oldFILE="$IN_DATE""$IN_FILE"
echo "oldfile ${oldFILE}"
origFILE=$(echo ${oldFILE} | awk '{print "\042"$0"\042"}')
echo "origfile ${origFILE}"

AND

oldFILE="$IN_DATE""$IN_FILE"
echo "oldfile ${oldFILE}"
origFILE=$(echo ${oldFILE} | awk '{print "\047"$0"\047"}')
echo "origfile ${origFILE}"

the echo shows the single or double quotes around the file name but I still get the same error on the mv.
Code:
oldfile 20180419_test with space.txt
origfile "20180419_test with space.txt"

oldfile 20180419_test with space.txt
origfile '20180419_test with space.txt'

I've also tried escaping the spaces with a \ but so far nothing that I've tried has worked. I get the same error. From the command line the mv works using ' or " or escaping the space with \.

Any suggestions?

Last edited by w_s_s; 04-20-2018 at 01:27 PM.. Reason: snytax
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How can i remove spaces in between the fields in a file

Hey , I have a file and it's having spaces for some of the fields in it. Like the one below. I want to remove the spaces in them through out the file. The spaces occur randomly and i can't say which field is having space. So please help. Here is sample file with spaces after 5th field. (3 Replies)
Discussion started by: dsravan
3 Replies

2. Shell Programming and Scripting

Remove spaces between file names

Hi All, I have spaces in between file names. "Material Header.txt" "Customer Header.txt" "Vendor Header.txt" And how can I remove spaces between file names like below MaterialHeader.txt CustomerHeader.txt VendorHeader.txt Thanks Srimitta (10 Replies)
Discussion started by: srimitta
10 Replies

3. Shell Programming and Scripting

Shell Script to remove spaces while writing to the file

Hello Folks, I want to get the results from a SQL query which needs to be exported to a .txt file. My Script is something like #!/bin/ksh db2 connect to DATABASE user user_name using pwd; touch test.txt isResult=0; isResult= `db2 -x select 'ABC',COL_B from TABLE_A WHERE COL_B=CONDITION`... (6 Replies)
Discussion started by: dinesh1985
6 Replies

4. Shell Programming and Scripting

Remove trailing spaces from file

I'm currently writing my sql results to a file and they have trailing spaces after each field. I want to get rid of these spaces and I'm using this code: TVXTEMP=$(echo $TVXTEMP|sed -e 's/\ //g') It doesn't work though. I'm not familiar with sedscript, and the other codes I've found online... (6 Replies)
Discussion started by: avillanueva
6 Replies

5. UNIX for Dummies Questions & Answers

remove spaces in between file

hey, I have this file: ATOM 2510 HG12 VAL 160 8.462 15.861 1.637 ATOM 2511 HG13 VAL 160 9.152 14.510 0.725 ATOM 2512 CG2 VAL 160 6.506 16.579 -0.088 ATOM 2513 HG21 VAL 160 5.499 16.421 -0.478 ATOM 2514 HG22 VAL 160 6.417 16.984 ... (4 Replies)
Discussion started by: kanikasharma
4 Replies

6. Shell Programming and Scripting

Remove spaces / tabs from variable in script

I want to remove extra spaces from variable in aix script. We retrieve the data from oracle database and then print the values. We have a value on 90th position. When we execute the query on sqlplus it shows the length of 90th position as 3, but when we use the same query in aix script it shows... (5 Replies)
Discussion started by: lodhi1978
5 Replies

7. Linux

How to remove spaces in the file?

hiii i have a file that contains spaces in the begining of a file till the middle the from there the txt would appear. hw can i remove those spaces and bring the text to the begining portion file1 text starts from here (12 Replies)
Discussion started by: anurupa777
12 Replies

8. Shell Programming and Scripting

Remove spaces in a file

Hi friends, I have a file1.txt 1 | a | 4757634 | jund jdkj | erhyj 2 | a | 4757634 | jnd jdkj | rhje hjrhwj i have used tr -d '\040' to remove the spcaes output file cat file1.txt | tr -d '\040' 1|a|4757634|jundjdkj|erhyj... (5 Replies)
Discussion started by: i150371485
5 Replies

9. Shell Programming and Scripting

How to remove spaces between the columns in UNIX script?.

Hi guru's, I am trying to write a script to generate a csv file by connecting to database run a query and put the values into csv file. But the problem i face is i am getting lot of space after one value.how can i remove those values?. Please help. #!/bin/bash export... (2 Replies)
Discussion started by: karingulanagara
2 Replies

10. Shell Programming and Scripting

Remove spaces from the file

Hi All, The output file contains data as below. "20141023","CUSTOMER" ,"COMPANY" ,"IN0515461" ,"" ,"JOSHUA" There are spaces in between the ending " and ,. The number of spaces is random. How can I remove that from the file so that the final output is:... (4 Replies)
Discussion started by: aarsh.dave
4 Replies
MSSQL_FETCH_FIELD(3)													      MSSQL_FETCH_FIELD(3)

mssql_fetch_field - Get field information

SYNOPSIS
object mssql_fetch_field (resource $result, [int $field_offset = -1]) DESCRIPTION
mssql_fetch_field(3) can be used in order to obtain information about fields in a certain query result. PARAMETERS
o $result - The result resource that is being evaluated. This result comes from a call to mssql_query(3). o $field_offset - The numerical field offset. If the field offset is not specified, the next field that was not yet retrieved by this function is retrieved. The $field_offset starts at 0. RETURN VALUES
Returns an object containing field information. The properties of the object are: o name - column name. if the column is a result of a function, this property is set to computed#N, where #N is a serial number. o column_source - the table from which the column was taken o max_length - maximum length of the column o numeric - 1 if the column is numeric o type - the column type. EXAMPLES
Example #1 mssql_fetch_field(3) example <?php // Connect to MSSQL and select the database mssql_connect('MANGOSQLEXPRESS', 'sa', 'phpfi'); mssql_select_db('php'); // Send a select query to MSSQL $query = mssql_query('SELECT * FROM [php].[dbo].[persons]'); // Construct table echo '<h3>Table structure for 'persons'</h3>'; echo '<table border="1">'; // Table header echo '<thead>'; echo '<tr>'; echo '<td>Field name</td>'; echo '<td>Data type</td>'; echo '<td>Max length</td>'; echo '</tr>'; echo '</thead>'; // Dump all fields echo '<tbody>'; for ($i = 0; $i < mssql_num_fields($query); ++$i) { // Fetch the field information $field = mssql_fetch_field($query, $i); // Print the row echo '<tr>'; echo '<td>' . $field->name . '</td>'; echo '<td>' . strtoupper($field->type) . '</td>'; echo '<td>' . $field->max_length . '</td>'; echo '</tr>'; } echo '</tbody>'; echo '</table>'; // Free the query result mssql_free_result($query); ?> SEE ALSO
mssql_field_seek(3). PHP Documentation Group MSSQL_FETCH_FIELD(3)
All times are GMT -4. The time now is 09:46 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy