Register
It is currently Wed Jul 23, 2014 9:49 pm

editing txt-file finding max value in a block


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Mon Jan 19, 2009 3:48 am   

Joined: Mon Jan 19, 2009 3:18 am
Posts: 1
Hello there,

I need a bash script to automatically edit a file for simulations (input2edit.inp, taken as input argument). This file (look at the bottom of this post) describes a "mesh" (set of nodes and elements) with
a. node list with coordinates
b. element list referring to the nodes

I need to:
1. sort the node list in ascending order and finding the highest node --> max-node
2. replacing line "*ELEMENT, TYPE=CPS3" with line "ELEMENT, TYPE=DC2D3"
3. adding following lines after line " *SOLID SECTION, ELSET=W, MATERIAL=W"

*Nset, nset=PF, instance=PART-1-1, generate
1, <max-node>, 1

4. attaching the textfile "k02TT.inp" at the end of the file and getting "input_ready.inp"

cat $1 k02TT.inp > input_ready.inp ???

Many thanks for help, especially for 1. and 3. !!!
Alex

Quote:

*NODE
3, 0.0, 0.0
1, 0.00225704804161, 0.0
6, 0.0051913319517, 0.0
7, 0.00723961685581, 0.0
21, 0.0107555460777, 0.0
19, 0.01306875, 0.0
132466, 0.0176946608117, 0.0
31, 0.0209391103626, 0.0
...
...
** The element type is T3_3. The type provided for ABAQUS is only a guess
** and may have to be modified by the user to be meaningful.
*ELEMENT, TYPE=CPS3
1, 1, 2, 3
2, 4, 1, 5
3, 5, 1, 6
4, 7, 5, 6
5, 8, 7, 9
6, 8, 5, 7
...
...
...
*SOLID SECTION, ELSET=W, MATERIAL=W
*MATERIAL, NAME=C
** Use the information in the header to complete these fields under MATERIAL
*MATERIAL, NAME=W
** Use the information in the header to complete these fields under MATERIAL


Top
 Profile  
 PostPosted: Mon Jan 19, 2009 4:08 pm   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
okey, this might be a over ambitious one ;P And I'm not even sure it'll work as you want it too... but with the information you've given it works :)
Code:
#!/bin/bash
tmp_file="/tmp/file.$$"
file="$1"
attachment="k02TT.inp"
output="input_ready.inp"

# Just a quickie function to get chunks of text from a file between 2 search parameters
function create_array {
   input=$1
   SOP=$2
   EOP=$3
   x=0
   y=0
   lines=$(cat $input | wc -l)
   while [ true ]; do
      [ $y -lt $lines ] || break
      string=$(head -n$y $input | tail -n1)
      echo "$string" | grep "$SOP" &> /dev/null
      if [ $? -eq 0 ]; then
         retval=true
         let y=y+1
         continue
      fi
      echo "$string" | grep "$EOP" &> /dev/null
      if [ $? -eq 0 ]; then
         retval=false
         break
      fi
      if $retval; then
         array[$x]=$string
         let x=x+1
      fi
      let y=y+1
   done
}

# Function to print out all that is in an array
function print_array {
   output_file=$1
   for i in $(seq 0 ${#array[*]}); do
      echo ${array[$i]} >> $output_file
   done
   unset array
}

# Just to check if all the files are present or not :)
if [ -f $output ]; then
   echo "Output file already exists";
   exit 1
fi
if [ ! -f $file ]; then
   echo "Input file does not exist";
   exit 1
fi
if [ ! -f $attachment ]; then
   echo -e "Attachment file does not exist. No data will be written at the end of the file.\n This is just a warning."
fi
echo -n "Processing $file ... ";

# Create an array containing everything in between "*NODE" and "** The element type is".
# You need to escape characters that can be interpreted by grep as regular expressions.
create_array $file "\*NODE" "\*\* The element type is"
# Print array to a temporary file
print_array $tmp_file
# Store the sorted values in a variable for safekeeping
sorted=$(cat $tmp_file | sort -k 2 -r)
# Remove the tmp file :)
rm $tmp_file

# Start formatting the output to the output file (defined at the top in variable "output")
echo -e "*NODE
$sorted
** The element type is T3_3. The type provided for ABAQUS is only a guess
** and may have to be modified by the user to be meaningful.
ELEMENT, TYPE=DC2D3" >> $output

# Create a new array with all the data contained in between "*ELEMENT" and "*SOLID SECTION"
create_array $file "\*ELEMENT" "\*SOLID SECTION"
# Print this directly to the output file, as described in your post there wasn't any sorting to be done
print_array $output
echo -e "*SOLID SECTION, ELSET=W, MATERIAL=W
*Nset, nset=PF, instance=PART-1-1, generate
1, <max-node>, 1
*MATERIAL, NAME=C
** Use the information in the header to complete these fields under MATERIAL
*MATERIAL, NAME=W
** Use the information in the header to complete these fields under MATERIAL" >> $output

# The attachment file is defined at the top in the variable attachment. This is the k02TT.inp file mentioned
cat $attachment >> $output

echo "DONE"


I've basically reconstructed the file from the ground up.
If you wan't it to be more automatic you could use my create_array to chop out another set of chunks.
And use print_array $output to get in into the file at the position you want it.

Best regards
Fredrik Eriksson


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP